无法解析JSON对象

时间:2018-03-26 18:30:29

标签: java json

我试图使用java解析JSON Object。我正在使用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我想检索并打印完整的Last6Months JSONObject。但我无法这样做。以下是我正在尝试的代码:

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();
        }
    }
}

在部分输出中我得到objobj json对象的1个大小。而我的预期大小为6.同样在keyset我得到[Last6Months]作为输出;而我期待[Jan 18, Feb 18, Dec 17, ... etc]。如何获得所需的输出以打印完整的Last6Months json对象。

1 个答案:

答案 0 :(得分:3)

objobj指向data数组中的第3个元素,让我们看一下:

        {
            "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"
                }
            }
        }

正如你在显微镜下看到的那样,这是一个带有1个键Last6Months的对象,所以它的大小为1,键集包括Last6Months

我认为你只比你想象的高1级。你可以试试

JSONObject objobj = (JSONObject) dataArr.get(3);
JSONObject last6Months = (JSONObject) objobj.get("Last6Months");
System.out.println(last6Months.size());
System.out.println(last6Months.keySet().toString());

您将看到密钥集和大小将符合您的预期