简体   繁体   English

用Java编写一个Json文件,嵌套对象而不是String

[英]Write a Json file in Java , nested objects instead of String

I have to read a sample pom file and write all technology and version in to json file, Im able to get the output in this format: 我必须阅读一个示例pom文件,并将所有技术和版本都写入json文件,我能够以这种格式获取输出:

["{ name:junit ,Version:4.12}","{ name:spring-batch-test ,Version:3.0}","{ name:spring-boot-starter }","{ name:slf4j-api }"]

However I want to get output in this format: 但是我想以这种格式输出:

[{ "name":"junit" ,"Version":"4.12"},{" name":"spring-batch-test" ,"Version":"3.0"},{"name":"spring-boot-starter" }]

My code : 我的代码:

Map<String, String> dependencies = Maps.newHashMap();
dependencies = populateProjectDepedencies(dependencies, pomFile);
In populateProjectDependencies
for (Dependency dependency : dependencyList) {
    String version = "0.0";
    if (dependency.getVersion() != null && 
        dependency.getVersion().startsWith("${"))
    {
        version = (String) properties.get(dependency.getVersion()
                  .substring(2, dependency.getVersion().length() - 1));
    } else {
        version = dependency.getVersion();
    }
    if (version != null) {
        String a1[]=version.split("\\.");
        int i=a1.length;
        if(i>=2)
        {
            version=a1[0]+"."+a1[1];
        }
        dependencies.put("{name:"+dependency.getArtifactId(),",
                          Version:"+version+"}" );
        JSONArray jsonArray = prepareJsonObject(dependencies);
        genarateTechnologyRadarJson(jsonArray);
        writer.write(jsonArray.toJSONString());

Because, you are adding the value as a String 因为,您将值添加为String

"{ name:"+dependency.getArtifactId(),"

Even, I'm not sure why are you manually constructing the JSON instead just pass the Map object to JSONObject . 甚至,我不确定您为什么要手动构造JSON,而只是将Map对象传递给JSONObject

JSONObject obj=new JSONObject(yourmap);

As I understand from your question, you are holding json as String array but you want to hold data as JSONObject array. 据我从您的问题中了解,您将json保留为String数组,但希望将数据保留为JSONObject数组。 So, 所以,

JSONArray ja = new JSONArray();
for (Dependency dependency : dependencyList) {
   .....
   JSONObject obj=new JSONObject();
   obj.put("name",dependency.getArtifactId()); 
   obj.put("Version",version);
   ja.put(obj);
   //remove dependencies.put,JSONArray. and genarateTechnologyRadarJson(jsonArray);
}
writer.write(ja.toString()); 

UPDATE 更新

This should be your complete code 这应该是您的完整代码

    JSONArray jsonArray = new JSONArray();
    for (Dependency dependency: dependencyList) {
        String version = "0.0";
        if (dependency.getVersion() != null &&
                dependency.getVersion().startsWith("${")) {
            version = (String) properties.get(dependency.getVersion()
                    .substring(2, dependency.getVersion().length() - 1));
        } else {
            version = dependency.getVersion();
        }
        if (version != null) {
            String a1[] = version.split("\\.");
            int i = a1.length;
            if (i >= 2) {
                version = a1[0] + "." + a1[1];
            }
        }
        JSONObject obj=new JSONObject();
        obj.put("name",dependency.getArtifactId());
        obj.put("Version",version);
        jsonArray.put(obj);
    }

    writer.write(jsonArray.toJSONString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM