0

I have this JSON structure:

{"metrics":[{
        "type": "sum",
        "column": ["rsales", "nsales"]
    },
    {
        "type":"count",
        "column":["ptype", "plan"]
    }]
}

I am trying to read that JSON from Java and want to the output to be like:

str_sum="Sum" 
str_sum_array[]= {"rsales" ,"nsales"} 
str_count="count" 
str_count_array[]= {"ptype" ,"plan"} 

Here is my code so far:

JSONArray jsonArray_Metric = (JSONArray) queryType.get("metrics");
for (int i = 0; i < jsonArray_Metric.length(); i++) {
JSONObject json_Metric = jsonArray_Metric.getJSONObject(i);
Iterator<String> keys_Metrict = json_Metric.keys();
while (keys_Metrict.hasNext()) {
    String key_Metric = keys_Metrict.next();
    // plz help
  }
}

How can I complete the code to produce the desired output?

ManuV
  • 31
  • 1
  • 4
  • 1
    It's usually easiest to write Java model classes and use Jackson for serialization / de-serialization. – EvanM Sep 09 '17 at 17:24

1 Answers1

1

Instead of using iterator you can use simple for-loop as below ..

JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(queryType);
JSONArray jsonArray_Metric = (JSONArray) object.get("metrics");
for (int index = 0; index < jsonArray_Metric.size(); index++) {
   JSONObject item = (JSONObject) jsonArray_Metric.get(index);
   String type = (String) item.get("type");
   JSONArray column = (JSONArray) item.get("column");
   System.out.println("str_sum store=\"" +  type + "\"");
   System.out.println("str_count_array[] store=" +  column);
} 

Sample Run

str_sum store="sum"
str_count_array[] store=["rsales","nsales"]
str_sum store="count"
str_count_array[] store=["ptype","plan"]

If you want JSONArray to be displayed with curly braces instead of default (actual) braces i.e. square braces then you could so something like this while printing or you can even delete them by replacing them with empty string "".

System.out.println("str_count_array[] store " +  column.toString().replace("[", "{").replace("]", "}"));

You can format your display code as you like by playing around with println statement.

JRG
  • 4,037
  • 3
  • 23
  • 34
  • Sorry i might not clear with the output i like it should be str_sum="Sum" str_sum_array[]= "rsales,nsales" str_count="count" str_count_array[]="ptype,plan" – ManuV Sep 09 '17 at 17:34
  • first variable str_sum stroe "sum" second array str_sum_array[] should store ="rsales,nsales" third variable str_count store "count" and finally fourth str_count_array[] should store "ptyp,plan" – ManuV Sep 09 '17 at 17:43
  • I put the curly braces in the question. I assumed it was the Java object that was wanted, not the output – OneCricketeer Sep 09 '17 at 17:45
  • 1
    @ManuV this answer is fine. You can format the output however – OneCricketeer Sep 09 '17 at 17:45
  • @JRG it says java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.JSONObject – ManuV Sep 09 '17 at 19:19
  • That's because we are using JSONObject Class from different packages/libraries. Check your imports to only use JSONObject from one package. – JRG Sep 09 '17 at 19:21