我正在使用spring boot并且生成了一个json对象。
[
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"},
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"},
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"}
]
我必须区分日期并获得一个新的json对象,如下面的
[
{"date":"2017-11-20T17:12:01.340", "name":"abc", "feedback_1":"good", "feedback_2":"bad", "feedback_3":"poor", "category_1":"AA", "category_2":"BB", "category_3":"CC"},
{"date":"2017-12-15T16:53:02.042", "name":"xyz", "feedback_1":"very bad", "feedback_2":"nice", "feedback_3":"poor", "category_1":"AA", "category_2":"XX", "category_3":"YY"}
]
我尝试用Java或JavaScript获取此格式。我尽力使用在线资源,但由于我是JavaScript的新手,我失败了,我该如何解决?提前致谢。
答案 0 :(得分:1)
您可以使用array#reduce
创建对象并添加计数器以记录JSON的频率,并使用此计数器添加feedback
和category
。之后,从结果中删除计数器。
var data = [ {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"} ];
var result = Object.values(data.reduce((r,o) => {
var key = o.data + "|" + o.name;
r[key] = r[key] || {date: o.date, name: o.name, count:1};
r[key] = Object.assign({}, r[key], {['feedback_'+r[key].count] : o.feedback, ['category_'+r[key].count]: o.category, count : r[key].count+1});
return r;
},{}));
result.forEach(o => delete o.count);
console.log(result);
答案 1 :(得分:1)
您可以尝试linq.js库:
var data = [{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"}, {"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}, {"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"}];
var temp = Enumerable.From(data).GroupBy("$.date").Select("{date: $.Key(), name: $.First().name, feedback: Enumerable.From($).Select('$.feedback').ToArray(), category: Enumerable.From($).Select('$.category').ToArray() }").ToArray();
for (var item of temp) {
for (var prop of ['feedback', 'category']) {
for (var x of item[prop])
item[prop + '_' + (item[prop].indexOf(x) + 1)] = x
delete item[prop];
}
}
console.log(temp);

<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>
&#13;
答案 2 :(得分:0)
拿2个阵列
1st array={"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"good","category":"AA"},
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"bad","category":"BB"},
{"date":"2017-11-20T17:12:01.340","name":"abc","feedback":"poor","category":"CC"}
2nd={"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"very bad","category":"AA"}
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"nice","category":"XX"}
{"date":"2017-12-15T16:53:02.042","name":"xyz","feedback":"okey","category":"YY"}
并使用array_unique获取唯一值