从java中的数组列表多映射创建嵌套的Json

时间:2015-05-15 15:37:00

标签: java json arraylist hashmap

我有一个数组列表多地图 -

ArrayListMultimap<String, String> fcmbuildProperties = ArrayListMultimap.create();
ArrayListMultimap<String, String> scm = ArrayListMultimap.create();
//HashMap<String, String> fcmbuildProperties= new HashMap<String, String>();
fcmbuildProperties.put("name", buildName);
fcmbuildProperties.put("timestamp", buildTimeStamp);
fcmbuildProperties.put("groupId", groupID);
fcmbuildProperties.put("artifactId", artifactID);
fcmbuildProperties.put("version", version);
fcmbuildProperties.put("environment", environment);
fcmbuildProperties.put("number", patch);
fcmbuildProperties.put("artifactPath", layoutPath);
fcmbuildProperties.put("architecture", architecture);
fcmbuildProperties.put("operatingSystem", operatingSystem);
scm.put("commit",commitSha);
scm.put("url", githubUrl);
scm.put("branch", githubBranch);

这显示为json,如下所示:

{
  "operatingSystem": [
    "rhel5"
  ],
  "artifactPath": [
    "djin/platform/cache/test/rhel5/i386/packages/test/test-1.0.0-d.284.i386.rpm"
  ],
  "artifactId": [
    "test"
  ],
  "number": [
    "284"
  ],
  "architecture": [
    "i386"
  ],
  "url": [
    null
  ],
  "version": [
    "1.0.0"
  ],
  "timestamp": [
    "6/4/15/2015/11:22:7"
  ],
  "groupId": [
    "cache"
  ],
  "environment": [
    "snapshot"
  ],
  "commit": [
    null
  ],
  "name": [
    "fcm-dummy-web"
  ],
  "branch": [
    null
  ]
}

但我需要将其解析为:

{
    "name": "fcm-dummy-web",
    "url": "job/fcm-dummy-web/",
    "build": {
        "full_url": "job/fcm-dummy-web/29/",
        "number": 38,
        "url": "job/fcm-dummy-web/29/",
        "scm": {
            "url": "institutional/fcm-dummy-web",
            "branch": "origin/master",
            "commit": "989f0b78470f0dc9e262cc020e66837beef16c4e"
        },
        "artifacts": {
            "id": "test",
            "groupId": "djin/platform/cache",
            "operatingSystem": "rhel5",
            "environment": "snapshot",
            "path": "/cache/test/rhel5/i386/packages/testtest-1.0.0-d.269.i386.msi",
            "architecture": "i386",
            "version": "1.0.0"
        }
    }
}

嵌套json比扁平结构更多。什么是有效的方法来实现这一目标?

我使用gson从arrayListMultimap制作儿子:

Gson gson = new Gson();
String jsonString = gson.toJson(fcmbuildProperties.asMap()); 

2 个答案:

答案 0 :(得分:2)

这取决于您使用哪个库进行JSON序列化。

许多图书馆提供&#34;漂亮的印刷&#34;这是一个常见的属性:

Pretty printing JSON from Jackson 2.2's ObjectMapper

Pretty-Print JSON in Java

编辑:使用Gson,这很简单

Gson gson = new GsonBuilder().setPrettyPrinting().create();

创建Gson对象时。

编辑2:

public static class OuterObject {
    String name;
    String url;

    BuildProperties properties;

}

public static class BuildProperties {
    String full_url;
    int number;
    Map<String, String> scm = new HashMap<String, String>() {{
        put("url":"institutional/fcm-dummy-web");
        //etc
    }};

}

答案 1 :(得分:0)

我假设ArrayListMultiMap来自Google Guava库。

如果是这样,你的代码并没有构建一个深度结构化的对象,所以GSON也不会显示更深层次的结构。

这样做的一种方法是使用这样的法线贴图:

Map<String, Object> scm = new LinkedListMap<>();
scm.put("url", "institutional/fcm-dummy-web");
// TODO put the rest of the properties

Map<String, Object> artifacts = new LinkedListMap<>();
// TODO put the corresponding properties

Map<String, Object> build = new LinkedListMap<>();
// TODO put the props
build.put("scm", scm);
build.put("artifacts", artifacts);

Map<String, Object> root = new LinkedListMap<>();
root.put("name", "fcm-dummy-web");
root.put("url", "job/fcm-dummy-web/");
root.put("build", build);

现在您可以使用GSON打印此地图:

new GSON().toJson(root);

这里有几点需要注意: 1.我们正在使用LinkedListMap,因此元素的显示顺序与我们放置的顺序相同。您可以使用TreeMap来代替按名称排序。使用普通HashMap也可以,但每次运行此代码时,您可能会得到不同的顺序。 2.地图中的键是字符串,但有些值是字符串和其他 - 地图。所以我们必须使用Map,从而失去类型检查。

很多时候,更好的方法是为对象创建POJO类。这是一种方法:

public class BuildProperties {
    private String name;
    private Sting uri;
    private Build build;

    // TODO write getters and setters
}

public class Build {
    private String full_url;
    // ....
    private Scm scm;
    private Artifacts artifacts;

    // TODO write getters and setters
}

// TODO write the Scm and Artifacts classes

困扰我的一件事是Artifacts部分。您的JSON在那里只有一个工件。您可能需要的是它看起来像这样:

"artifacts": [
    {
        "id": "test",
        "groupId": "djin/platform/cache",
        "operatingSystem": "rhel5",
        "environment": "snapshot",
        "path": "/cache/test/rhel5/i386/packages/testtest-1.0.0-d.269.i386.msi",
        "architecture": "i386",
        "version": "1.0.0"
    }
]

要使用地图代码获取此信息,您必须更改它:

Map<String, Object> artifact1 = new LinkedListMap();
artifact1.put("id", "test");
// ...
List<Map<String, Object>> artifacts = new ArrayList<>();
artifacts.add(artifact1);

使用POJO代码,Build类应该如下所示:

public class Build {
    private String full_url;
    // ....
    private Scm scm;
    private List<Artifact> artifacts;

    // TODO write getters and setters
}

对于单个工件,类应为Artifact