我现在有一个如下的响应,我希望它在一行中完成。
当前回复:
[
{
"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 }
等等……对于每一个回应。。
我尝试了很多事情,但都没能成功。请指导,我如何才能做到以上。。
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}
您的问题不清楚。告诉我这个输出是否是您想要的。
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":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" +
" }" +
" }" +
"]");
转换
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)");
for (JsonNode elem : node) {
System.out.println(elem.toString());
}
输出
{"user_id":30826889,"timestamp":"1664325660","hr":65,"rr":18,"snoring":0}
{"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":"1664340780","hr":72,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664340840","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664340900","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664340960","hr":70,"rr":20,"snoring":0}
{"user_id":30826889,"timestamp":"1664341020","hr":67,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341080","hr":71,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341140","hr":69,"rr":19,"snoring":0}
{"user_id":30826889,"timestamp":"1664341200","hr":68,"rr":21,"snoring":0}
{"user_id":30826889,"timestamp":"1664341260","hr":66,"rr":22,"snoring":0}
{"user_id":30826889,"timestamp":"1664341320","hr":68,"rr":22,"snoring":0}
以下是Javascript中的一个解决方案:
const inputArray = [
{
"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 }
for (const { user_id, hr, rr, snoring } of inputArray) {
for (const timestamp of Object.keys(hr)) {
const result = { user_id, timestamp, hr: hr[timestamp], rr: rr[timestamp], snoring: snoring[timestamp] };
console.log(JSON.stringify(result));
}
}