无法解析JSON对象

JPG格式

我试图JSON Object使用Java进行解析我正在使用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中,我想检索并打印complete 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();
        }
    }
}

在部分输出中,我得到objobjjson对象1的大小而我预期大小为6。此外,在keyset我得到[Last6Months]的输出; 而我期望[Jan 18, Feb 18, Dec 17, ... etc]如何获得所需的输出以打印完整的Last6Monthsjson对象。

安德鲁·洛尔

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

        {
            "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个带有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());

然后您会看到键集和大小将是您期望的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章