【发布时间】:2015-07-27 14:43:32
【问题描述】:
我有一个数组列表多图 -
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());
【问题讨论】:
-
什么是
ArrayListMultiMap? -
您希望它自动完成还是通过一一添加来创建 JSON?
-
ChetanKinger - 我相信你可以用谷歌搜索。 @DAO - 自动会很棒,或者我可以一一完成。
-
@Scooby,
ArrayListMultiMap不是标准类。请指定我们可以在哪里看到它。此外,您的代码会创建scm并在其中放入一些元素,但它从未附加到fcmbuildProperties。
标签: java json arraylist hashmap