如何反序列化具有动态键值对的Json响应

wnrlj8wa  于 2022-10-22  发布在  Java
关注(0)|答案(2)|浏览(167)

我现在有一个如下的回复,我希望它能在一行中完成。
当前回复:

[
   {
      "user_id":30826889,
      "hr":{
         "1664325660":65,
         "1664325720":65,
         "1664325780":70
      },
      "rr":{
         "1664325660":18,
         "1664325720":17,
         "1664325780":15
      },
      "snoring":{
         "1664325660":0,
         "1664325720":0,
         "1664325780":0
      }
   },
   {
      "user_id":30826889,
      "hr":{
         "1664340780":72,
         "1664340840":70,
         "1664340900":71,
         "1664340960":70,
         "1664341020":67,
         "1664341080":71,
         "1664341140":69,
         "1664341200":68,
         "1664341260":66,
         "1664341320":68
      },
      "rr":{
         "1664340780":20,
         "1664340840":20,
         "1664340900":19,
         "1664340960":20,
         "1664341020":19,
         "1664341080":19,
         "1664341140":19,
         "1664341200":21,
         "1664341260":22,
         "1664341320":22
      },
      "snoring":{
         "1664340780":0,
         "1664340840":0,
         "1664340900":0,
         "1664340960":0,
         "1664341020":0,
         "1664341080":0,
         "1664341140":0,
         "1664341200":0,
         "1664341260":0,
         "1664341320":0
      }
   }
]

等等
我希望如下所示,使其成为键值对。这样地;

{"user_id": 30826889, "timestamp": "166432xxxx","hr":65, "rr":45, "snoring":1 }
{"user_id": 30826889, "timestamp": "166432yyyy","hr":67, "rr":23, "snoring":2 }

以此类推……对于每个回应。。
我尝试了很多事情,但都没有成功。请指导,我如何实现上述目标。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

js4nwp54

js4nwp541#

你的问题不清楚。告诉我这个输出是否是您想要的。
https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "{" +
    "    \"user_id\": 30826889," +
    "    \"hr\": {" +
    "        \"1664325660\": 65," +
    "        \"1664325720\": 65," +
    "        \"1664325780\": 70" +
    "    }," +
    "    \"rr\": {" +
    "        \"1664325660\": 18," +
    "        \"1664325720\": 17," +
    "        \"1664325780\": 15" +
    "    }," +
    "    \"snoring\": {" +
    "        \"1664325660\": 0," +
    "        \"1664325720\": 1," +
    "        \"1664325780\": 2" +
    "    }" +
    "}");
JsonNode node = josson.getNode(
    "map(user_id," +
    "    items: collect(hr.entries().map(key, hr:value)," +
    "                   rr.entries().map(key, rr:value)," +
    "                   snoring.entries().map(key, snoring:value))" +
    "           .flatten()" +
    "           .group(key, field(key:))" +
    "           .map(timestamp:key, elements.hr[0], elements.rr[0], elements.snoring[0])" +
    ")" +
    ".unwind(items)");
System.out.println(node.toPrettyString());

System.out.println("\nLoop through each array element");
for (JsonNode elem : node) {
    System.out.println("-----");
    System.out.println(elem.toPrettyString());
}

输出

[ {
  "user_id" : 30826889,
  "timestamp" : "1664325660",
  "hr" : 65,
  "rr" : 18,
  "snoring" : 0
}, {
  "user_id" : 30826889,
  "timestamp" : "1664325720",
  "hr" : 65,
  "rr" : 17,
  "snoring" : 1
}, {
  "user_id" : 30826889,
  "timestamp" : "1664325780",
  "hr" : 70,
  "rr" : 15,
  "snoring" : 2
} ]

Loop through each array element
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325660",
  "hr" : 65,
  "rr" : 18,
  "snoring" : 0
}
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325720",
  "hr" : 65,
  "rr" : 17,
  "snoring" : 1
}
-----
{
  "user_id" : 30826889,
  "timestamp" : "1664325780",
  "hr" : 70,
  "rr" : 15,
  "snoring" : 2
}
oxf4rvwz

oxf4rvwz2#

public static void main(String... args) throws IOException {
    File src = new File("e:/in.json");
    ObjectMapper mapper = new ObjectMapper();
    List<Map<String, Object>> items = mapper.readValue(src, new TypeReference<>() {
    });

    for (Map<String, Object> map : convert(items))
        System.out.println(mapper.writeValueAsString(map));
}

public static List<Map<String, Object>> convert(List<Map<String, Object>> items) {
    return items.stream()
                .flatMap(item -> convert(item).stream())
                .collect(Collectors.toList());
}

private static List<Map<String, Object>> convert(Map<String, Object> item) {
    return getUniqueTimes(item.values()).stream()
                                        .map(time -> createTimeItem(item, time))
                                        .collect(Collectors.toList());
}

private static Set<String> getUniqueTimes(Collection<Object> values) {
    return values.stream()
                 .filter(value -> value instanceof Map)
                 .flatMap(value -> ((Map<String, Integer>)value).keySet().stream())
                 .collect(Collectors.toSet());
}

private static Map<String, Object> createTimeItem(Map<String, Object> item, String timestamp) {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("user_id", item.get("user_id"));
    map.put("timestamp", timestamp);

    item.entrySet().stream()
        .filter(entry -> entry.getValue() instanceof Map)
        .forEach(entry -> map.put(entry.getKey(), ((Map<String, Integer>)entry.getValue()).get(timestamp)));

    return map;
}

演示

{"user_id":30826889,"timestamp":"1664325720","hr":65,"rr":17,"snoring":0}
{"user_id":30826889,"timestamp":"1664325780","hr":70,"rr":15,"snoring":0}
{"user_id":30826889,"timestamp":"1664325660","hr":65,"rr":18,"snoring":0}
{"user_id":30826889,"timestamp":"1664340900","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341260","hr":66,"rr":22,"snoring":0}
{"user_id":30826889,"timestamp":"1664340840","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664341080","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341320","hr":68,"rr":22,"snoring":0}
{"user_id":30826889,"timestamp":"1664341200","hr":68,"rr":21,"snoring":0}
{"user_id":30826889,"timestamp":"1664340960","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664341140","hr":69,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341020","hr":67,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664340780","hr":72,"rr":20,"snoring":0}

相关问题