我有一个包含以下数据的JSON对象。
{
"1":[{"count":1,"sessionID":"111","timeLogin":2}],
"2":[{"count":1,"sessionID":"222","timeLogin":3}],
"3":[{"count":1,"sessionID":"333","timeLogin":3}],
"4":[{"count":1,"sessionID":"444","timeLogin":3}],
"5":[{"count":1,"sessionID":"555","timeLogin":3}]
}
我想产生一个结果-每次登录都要计数。
我的预期结果应该是
{"timeLogin":2, "count":1},
{"timeLogin":3, "count":4}
我可以对如何或怎样使用Java技术来计算值以产生预期结果有任何想法吗?
谢谢。
答案 0 :(得分:0)
这是一个两步法。首先计算数据,然后生成结果对象。
如果数据集数组中有多个对象,则必须使用另一个循环而不是硬编码:
const item = item1[0]
const data = {
"1": [{
"count": 1,
"sessionID": "111",
"timeLogin": 2
}],
"2": [{
"count": 1,
"sessionID": "222",
"timeLogin": 3
}],
"3": [{
"count": 1,
"sessionID": "333",
"timeLogin": 3
}],
"4": [{
"count": 1,
"sessionID": "444",
"timeLogin": 3
}],
"5": [{
"count": 1,
"sessionID": "555",
"timeLogin": 3
}]
}
// First create map of time to count
const counts = {} // time->count
Object.values(data).forEach(item1 => {
const item = item1[0]
if (counts[item.timeLogin] === undefined) {
counts[item.timeLogin] = item.count;
} else {
counts[item.timeLogin] += item.count;
}
});
console.log('counts', counts)
// Next generate an object
const res = {}
for (const key of Object.keys(counts)) {
res['timeLogin' + key] = counts[key]
}
console.log(res)
// Or generate an array of objects (as you requested)
const res1 = []
for (const key of Object.keys(counts)) {
res1.push({
['timeLogin' + key]: counts[key]
})
}
console.log(res1)
答案 1 :(得分:0)
这听起来像是杰克逊或其他JSON工具(如GSON等)的工作。
基本上,您必须首先将JSON转换为Java对象。有多种方法可以做到这一点。您可以编写自己的JSON POJO表示形式,使用JsonNode之类的通用JSON对象,也可以简单地使用Map<String, Object>
来完成工作。
然后您可以编写逻辑以使用对象将总数相加。
以下是使用Jackson的一些参考链接:
答案 2 :(得分:0)
private static String calculateTime(JSONObject obj, Connection conn, String db_type) {
String timeResult = null;
int nombor_seq = 1;
ArrayList<String> listTime = new ArrayList<String>();
ArrayList<String> listTimeSort = new ArrayList<String>();
JSONObject JSONmergedTime =new JSONObject();
JSONObject JSONmergedJSONTime = new JSONObject();
int objectLength = obj.length();
for(int uT=1; uT < objectLength+1; uT++) { //put time, session in one object
String str = Integer.toString(uT);
JSONArray arrCreateList=(JSONArray)obj.get(str);
JSONObject objSession;
for (Object roTime : arrCreateList) { //roTime
objSession = (JSONObject) roTime;
String StimeLoginManipulate = String.valueOf(objSession.get("timeLogin"));
int timeLoginManipulate = Integer.parseInt(StimeLoginManipulate);
String SsessionIDManipulate = String.valueOf(objSession.get("sessionID"));
JSONArray jArrayTimeManipulate = new JSONArray();
JSONObject objManipulate = new JSONObject();
objManipulate.put("timeLogin", timeLoginManipulate);
objManipulate.put("sessionID", SsessionIDManipulate);
jArrayTimeManipulate.put(objManipulate);
JSONObject jObjTimeManipulate = new JSONObject();
String SSEQ = Integer.toString(nombor_seq);
jObjTimeManipulate.put(SSEQ, jArrayTimeManipulate);
nombor_seq = nombor_seq + 1;
if (JSONmergedTime.isEmpty()) {
JSONmergedTime = jObjTimeManipulate;
JSONmergedJSONTime = JSONmergedTime; //merged for the first record
} else {
JSONmergedJSONTime = mergeJSONObjects(JSONmergedJSONTime, jObjTimeManipulate);
}
}//end for roTime
} //end for uT
System.out.println("JSONmergedJSONTime : "+JSONmergedJSONTime);
for(int uT=1; uT < objectLength+1; uT++) { //put time, session in one object
String str = Integer.toString(uT);
JSONArray arrCalList=(JSONArray)JSONmergedJSONTime.get(str);
JSONObject objCal;
for (Object roTime : arrCalList) { //roTime
objCal = (JSONObject) roTime;
String timeInsert = String.valueOf(objCal.get("timeLogin"));
listTime.add(timeInsert);
if (!listTimeSort.contains(timeInsert)) {
listTimeSort.add(timeInsert);
Collections.sort(listTimeSort);
}
}
}
//calculate count - I hard code it since it will be be only 4 result.For non fix value, I perform for loop for timeSort and time list.
int lengthTime = listTime.size();
int lengthTimeSort = listTimeSort.size();
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
Collections.sort(listTime);
for (int y = 0; y < lengthTime; y++ ) {
String listT = listTime.get(y);
int listIntT = Integer.parseInt(listT);
if (listIntT == 1) {
count1 = count1+1;
}
if (listIntT == 2) {
count2 = count2+1;
}
if (listIntT == 3) {
count3 = count3+1;
}
if (listIntT == 4) {
count4 = count4+1;
}
}
//end utk kira
JSONArray jArrayTime = new JSONArray();
JSONObject jObjTime = new JSONObject();
//ltk list sort sebagai object
for (int x = 0; x < lengthTimeSort; x++ ) {
JSONObject objCalculateTime = new JSONObject();
String timeSortValue = listTimeSort.get(x);
int timeLogin = Integer.parseInt(timeSortValue);
objCalculateTime.put("entry",timeLogin);
if (timeLogin == 1) {
objCalculateTime.put("count",count1);
}
if (timeLogin == 2) {
objCalculateTime.put("count",count2);
}
if (timeLogin == 3) {
objCalculateTime.put("count",count3);
}
if (timeLogin == 4) {
objCalculateTime.put("count",count4);
}
jArrayTime.put(objCalculateTime);
} //end for
System.out.println("jArrayTime : "+jArrayTime);
jObjTime.put("1", jArrayTime);
System.out.println("jObjTime: "+jObjTime);
return timeResult;
答案 3 :(得分:0)
listTimeSort.add(sTime);
String[] timeArr = new String[listTimeSort.size()];
timeArr = listTimeSort.toArray(timeArr );
List aListTime = Arrays.asList(timeArr);
Set<String> mySetTime = new HashSet<String>(aListTime);
for(String time1: mySetTime){
JSONObject objCalculateTime = new JSONObject();
objCalculateTime.put("entry",time1);
objCalculateTime.put("count",Collections.frequency(aListTime,time1));
jArrayTime.put(objCalculateTime);
}