将外层的other去掉 保留原始数据
把里面的json取出来,赋值给外面的json,再删除里面的json。我稍后写个demo
<script>
const data = {
"id": 1,
"other": {
"workscore-1": 10,
"workscore-2": 20
},
"name": "AAA"
}
function fun() {
for (let key in data.other) {
data[key] = data.other[key];
}
delete data["other"]
}
fun()
console.log(data);
</script>
const newDatas = datas.map(item => {
const obj = {
...item,
...item.other
}
Reflect.deleteProperty(obj, 'other')
return obj
})
在 java 中,fastjson 只有这一种处理方式。
public static void main(String[] args) {
String json = "{\n" +
" \"datas\":[\n" +
" {\n" +
" \"finalScore\":59.8,\n" +
" \"id\":13,\n" +
" \"kpiScore\":95,\n" +
" \"other\":{\n" +
" \"workScore_13\":80,\n" +
" \"workScore_44\":87,\n" +
" \"wokrScore_13\":5,\n" +
" \"workScore_42\":67,\n" +
" \"workScore_43\":67,\n" +
" \"wokrScore_44\":56,\n" +
" \"wokrScore_43\":67,\n" +
" \"wokrScore_42\":78\n" +
" },\n" +
" \"performanceScore\":45.5,\n" +
" \"userName\":\"郝巍巍\",\n" +
" \"workAttitude\":14.3\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonObject = JSON.parseObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("datas");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject data = jsonArray.getJSONObject(i);
String other = data.getJSONObject("other").toString(SerializerFeature.PrettyFormat);
other = other.substring(other.indexOf('\n') + 1);
other = other.substring(0, other.lastIndexOf('\n'));
System.out.println(other.replace("\t", ""));
// 或者是
String other1 = data.getJSONObject("other").toString(SerializerFeature.PrettyFormat);
System.out.println(other1.substring(1, other1.length() - 1).replace("\t", ""));
}
}
try {
JsonNode root = new ObjectMapper().readTree(json);
JsonNode inputs = root.path("datas").path(0);
System.out.println(inputs.path("other").asText());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
15 回答8.2k 阅读
8 回答6k 阅读
4 回答4.5k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
4 回答4k 阅读
3 回答3.6k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
终于解决了