Java 无法解析JSON对象
我试图用java解析JSON对象。我正在使用Java 无法解析JSON对象,java,json,Java,Json,我试图用java解析JSON对象。我正在使用org.json.simple库 我要解析的json { "usageHistory": { "apiKey": "--redacted--", "data": [ { "Yesterday": { "ActivePower": "43.730000000000004", "Re
org.json.simple
库
我要解析的json
{
"usageHistory": {
"apiKey": "--redacted--",
"data": [
{
"Yesterday": {
"ActivePower": "43.730000000000004",
"RemainingBalance": "2016469.050000007"
}
},
{
"Last Week": {
"ActivePower": "695.7100000000002",
"RemainingBalance": "5909672.770000028"
}
},
{
"Last Month": {
"ActivePower": "3816.950000000011",
"RemainingBalance": "1465006.8900000013"
}
},
{
"Last6Months": {
"Jan 18": {
"ActivePower": "69.69",
"RemainingBalance": "171922.6899999999"
},
"Oct 17": {
"ActivePower": "597.0699999999877",
"RemainingBalance": "1255754.1500000032"
},
"Dec 17": {
"ActivePower": "1.0600000000000007",
"RemainingBalance": "0.0"
},
"Feb 18": {
"ActivePower": "3816.950000000011",
"RemainingBalance": "1465006.8900000013"
},
"Sep 17": {
"ActivePower": "0.0",
"RemainingBalance": "-3.0"
},
"Nov 17": {
"ActivePower": "321.86000000000126",
"RemainingBalance": "1.7842124010000385E7"
}
}
}
]
}
}
在上面的json中,我想检索并打印最近6个月的completeJSONObject
。但我不能这样做。下面是我正在尝试的代码:
package pack;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Test2 {
public static void main(String[] args)
{
try {
String strJson = "{ \"usageHistory\": { \"apiKey\": \"05375636152205261225536573730\", \"data\": [ { \"Yesterday\": { \"ActivePower\": \"43.730000000000004\", \"RemainingBalance\": \"2016469.050000007\" } }, { \"Last Week\": { \"ActivePower\": \"695.7100000000002\", \"RemainingBalance\": \"5909672.770000028\" } }, { \"Last Month\": { \"ActivePower\": \"3816.950000000011\", \"RemainingBalance\": \"1465006.8900000013\" } }, { \"Last6Months\": { \"Jan 18\": { \"ActivePower\": \"69.69\", \"RemainingBalance\": \"171922.6899999999\" }, \"Oct 17\": { \"ActivePower\": \"597.0699999999877\", \"RemainingBalance\": \"1255754.1500000032\" }, \"Dec 17\": { \"ActivePower\": \"1.0600000000000007\", \"RemainingBalance\": \"0.0\" }, \"Feb 18\": { \"ActivePower\": \"3816.950000000011\", \"RemainingBalance\": \"1465006.8900000013\" }, \"Sep 17\": { \"ActivePower\": \"0.0\", \"RemainingBalance\": \"-3.0\" }, \"Nov 17\": { \"ActivePower\": \"321.86000000000126\", \"RemainingBalance\": \"1.7842124010000385E7\" } } } ] } }";
org.json.simple.JSONObject objMain = new org.json.simple.JSONObject();
JSONParser parser = new JSONParser();
objMain = (org.json.simple.JSONObject) parser.parse(strJson);
org.json.simple.JSONObject obj_usageHistory = (org.json.simple.JSONObject) objMain.get("usageHistory");
JSONArray dataArr = (JSONArray)obj_usageHistory.get("data");
JSONObject objobj = (JSONObject) dataArr.get(3);
System.out.println(objobj.size());
System.out.println(objobj.keySet().toString());
/*Set<String> set = objLast6Months.keySet();
for(String s:set) {
System.out.println(s);
org.json.simple.JSONObject obj = (org.json.simple.JSONObject)objLast6Months.get(s);
System.out.println("== "+s+" ==");
System.out.println(obj.get("ActivePower"));
System.out.println(obj.get("RemainingBalance"));
}*/
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
包装;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
公共类Test2{
公共静态void main(字符串[]args)
{
试一试{
字符串strJson=“{”usageHistory\”:{“apiKey\”:“0537563615220526122553573730\”,“data\”:[{”昨天\“:{”动态功率\“:“43730000000000004\”,“RemainingBalance\”:“2016469.05000007\”,{”上周\“:{”动态功率\“:“695.71000000000002\”,“RemainingBalance\”:“590967700028\”,{”上月\动态功率“:”3816.950000000011“,”剩余余额“:”1465006.8900000013“,”最近6个月“:”1月18日“:”动态功率“:”69.69“,”剩余余额“:”171922.68999999999“,”10月17日“:”动态功率“:”597.069999999877“,”剩余余额“:”1255754.150000032“,”12月17日“,”动态功率“:”1.0600000000000007\,“剩余余额”:“0.0\”,“2月18日”:{“主动力”:“3816.950000000011\,“剩余余额”:“1465006.8900000013\,“9月17日”:{“主动力”:“0.0\,“剩余余额”:“-3.0\,“11月17日”:{“主动力”:“321.86000000000126\,“剩余余额”:“124780007\”;
org.json.simple.JSONObject objMain=new org.json.simple.JSONObject();
JSONParser=新的JSONParser();
objMain=(org.json.simple.JSONObject)parser.parse(strJson);
org.json.simple.JSONObject obj_usageHistory=(org.json.simple.JSONObject)objMain.get(“usageHistory”);
JSONArray dataArr=(JSONArray)obj_usageHistory.get(“数据”);
JSONObject objobj=(JSONObject)dataArr.get(3);
System.out.println(objobj.size());
System.out.println(objobj.keySet().toString());
/*Set Set=objLast6Months.keySet();
for(字符串s:set){
系统输出打印项次;
org.json.simple.JSONObject obj=(org.json.simple.JSONObject)objLast6Months.get;
System.out.println(“==”+s+“=”);
System.out.println(对象获取(“ActivePower”);
系统输出打印项次(对象获取(“剩余余额”);
}*/
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
在部分输出中,objobj
json对象的大小为1。而我预期的尺寸是6号。同样在keyset
中,我得到[Last6Months]
作为输出;而我期望的是[1月18日、2月18日、12月17日等]
。如何获得所需的输出以打印completeLast6个月的json对象。objobj
指向数据
数组中的第三个元素,让我们看看:
{
"Last6Months": {
"Jan 18": {
"ActivePower": "69.69",
"RemainingBalance": "171922.6899999999"
},
"Oct 17": {
"ActivePower": "597.0699999999877",
"RemainingBalance": "1255754.1500000032"
},
"Dec 17": {
"ActivePower": "1.0600000000000007",
"RemainingBalance": "0.0"
},
"Feb 18": {
"ActivePower": "3816.950000000011",
"RemainingBalance": "1465006.8900000013"
},
"Sep 17": {
"ActivePower": "0.0",
"RemainingBalance": "-3.0"
},
"Nov 17": {
"ActivePower": "321.86000000000126",
"RemainingBalance": "1.7842124010000385E7"
}
}
}
正如您在显微镜下看到的,这是一个具有一个键的对象,该键持续6个月
,因此大小为1是有意义的,并且该键集将包括持续6个月
我想你只比你想象的高了一级。你可以试试看
JSONObject objobj = (JSONObject) dataArr.get(3);
JSONObject last6Months = (JSONObject) objobj.get("Last6Months");
System.out.println(last6Months.size());
System.out.println(last6Months.keySet().toString());
您将看到键集和大小与您期望的一致