gpt4 book ai didi

java - 如何在java中没有任何JSONArray的情况下解析JSON?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:24 24 4
gpt4 key购买 nike

我有一个像这样的 json 文件:

{
"a1" : {
"Contestant 1 marks" : 44,
"Contestant 10 marks" : 30,
"Contestant 11 marks" : 15,
"Contestant 12 marks" : 35,
"Contestant 13 marks" : 28,
"Contestant 14 marks" : 8,
"Contestant 2 marks" : 35,
"Contestant 3 marks" : 30.899999618530273,
"Contestant 4 marks" : 39,
"Contestant 5 marks" : 45,
"Contestant 6 marks" : 47,
"Contestant 7 marks" : 17,
"Contestant 8 marks" : 37,
"Contestant 9 marks" : 35
},
"a2" : {
"Contestant 1 marks" : 35,
"Contestant 10 marks" : 30,
"Contestant 11 marks" : 15,
"Contestant 12 marks" : 35,
"Contestant 13 marks" : 28,
"Contestant 14 marks" : 8,
"Contestant 2 marks" : 30,
"Contestant 3 marks" : 30.899999618530273,
"Contestant 4 marks" : 39,
"Contestant 5 marks" : 45,
"Contestant 6 marks" : 47,
"Contestant 7 marks" : 17,
"Contestant 8 marks" : 37,
"Contestant 9 marks" : 35
}
}

这里好像有两个JSON对象a1,a2。而这些对象本身就有14个JSON对象。

我有一个包含完整 JSON 的 StringcompleteJson;

现在当我写

JSONObject jsonRootObj = new JSONObject(completeJson);

那么json对象会进入jsonRootObj吗?

另外,我如何获取 JSON 文件中 a1 和 a2 的键值:“参赛者 1 标记”?

我想将 a1 中的对象的值与 a2 中具有相同 KEY 的对象的值相加。

最佳答案

这样你就可以从A1获取值:

long valA1 = ((Long)((JSONObject)jo.get("a1")).get("Contestant 1 marks")).longValue();

这样你就可以输入A2:

((JSONObject)((JSONObject)jo.get("a2")).get("Contestant 1 marks")).put("Contestant 1 marks", valA1);

乔在哪里:

JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject)jp.parse(<your json string>);

关于java - 如何在java中没有任何JSONArray的情况下解析JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com