当前位置: 首页 > 知识库问答 >
问题:

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

支洋
2023-03-14

我有一个阵列列表多地图-

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"
        }
    }
}
Gson gson = new Gson();
String jsonString = gson.toJson(fcmbuildProperties.asMap()); 

共有1个答案

邢灿
2023-03-14

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

许多图书馆都提供“漂亮的打印”,这是一个共同的属性:

从Jackson2.2的ObjectMapper打印JSON非常漂亮

Java中的漂亮JSON

编辑:使用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
    }};

}
 类似资料:
  • 我必须使用elasticsearch和python为需要索引的json数据创建索引。例如,我有一个嵌套数组[[39.909971141540645,1452077285.150548,1452077286.196072,1.0455241203308105]]我需要为这个数组定义一个映射,比如第一个字段是count,第二个字段是start_time,end_time,duration。请帮助说明如

  • 例如,有两个列表: 使用Stream,我想创建一个由这些列表组成的映射,其中清单1是键,清单2是值。要做到这一点,我需要创建一个辅助列表: list0按List1::Get和List2::Get的顺序使用。有没有一种不创建list0的更简单的方法?我尝试了以下代码,但没有起作用:

  • 我很少有像下面这样的课 我希望根据聚合项,对于每一个聚合,子项都应该通过以以下方式聚合:

  • 如何使用MapStruct映射嵌套列表? 下面是我的实体类: 我想把它映射到: 现在,我的映射器看起来像: 但它只映射“普通”属性,如Long、intger等,但它避免了嵌套列表。

  • 我如何在下面的场景中使用Mapstruct进行bean映射。 现在我想把sourceId映射到targetId,courseName映射到subjectName,studentName映射到memberName(list到list)。

  • 我需要在这里显示json响应