Reputation: 1253
I was trying to parse JSON Object
using java. I am using org.json.simple
library.
json that I want to parse
{
"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"
}
}
}
]
}
}
in above json I want to retrieve and print complete Last6Months
JSONObject
. But I am not able to do so. Below is my code that I am trying:
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();
}
}
}
in partial output I am getting 1 size for objobj
json object. whereas I expected size to be 6. Also in keyset
I am getting [Last6Months]
as output; whereas I expected [Jan 18, Feb 18, Dec 17, ... etc]
. How do I get desired output to print complete Last6Months
json object.
Upvotes: 1
Views: 117
Reputation: 5486
objobj
points to the 3rd element in the data
array so lets look at that:
{
"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"
}
}
}
as you can see here under the microscope this is 1 object with 1 key Last6Months
so it makes sense the size is 1 and the keyset would include Last6Months
.
I think you were just 1 level higher than you thought. You can try doing
JSONObject objobj = (JSONObject) dataArr.get(3);
JSONObject last6Months = (JSONObject) objobj.get("Last6Months");
System.out.println(last6Months.size());
System.out.println(last6Months.keySet().toString());
and you will see the keyset and size will be what you expect
Upvotes: 3