-4

I am new about JSON. I got the following json file from the server:

      [{"status":0,"hq":[["2018-04-20","11.51","11.35","-0.12","-1.05%","11.20","11.58","958691","109080.12","0.57%"],["2018-04-19","11.52","11.47","-0.03","-0.26%","11.42","11.69","849132","97959.35","0.50%"]],"code":"cn_000001","stat":["累计:","2018-04-19至2018-04-20","-0.15","-1.30%",11.2,11.69,1807823,207039.47,"1.07%"]}]

I need to get two records:

     1."2018-04-20","11.51","11.35","-0.12","-1.05%","11.20","11.58","958691","109080.12","0.57%"
     2."2018-04-19","11.52","11.47","-0.03","-0.26%","11.42","11.69","849132","97959.35","0.50%"

How can I parse it and with what java lib? Thanks for help!

ps. I have tried GSON, try to parse the JSON to map,

   Map<String, Object> map = new Gson().fromJson(result, new TypeToken<Map<String, Object>>(){}.getType());
                map.forEach((x,y)-> System.out.println("key : " + x + " , value : " + y));

And got the following exception:

  Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

So now I don't know how to do it. Please help. Thank you.

甄士隐
  • 29
  • 4
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – vikas kumar Apr 22 '18 at 12:08
  • Check these [link1](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) and [link2](https://www.geeksforgeeks.org/parse-json-java/), It got in-depth explanation. – Ram Apr 22 '18 at 12:09

1 Answers1

0

I would suggest to use this library : https://mvnrepository.com/artifact/org.json/json/20180130

I just write sample here for your reference :

JSONOArray jSONOArray = new JSONOArray([{"status":0,"hq":[["2018-04-20","11.51","11.35","-0.12","-1.05%","11.20","11.58","958691","109080.12","0.57%"],["2018-04-19","11.52","11.47","-0.03","-0.26%","11.42","11.69","849132","97959.35","0.50%"]],"code":"cn_000001","stat":["累计:","2018-04-19至2018-04-20","-0.15","-1.30%",11.2,11.69,1807823,207039.47,"1.07%"]}]);
 JSONObject jsonObj = jSONOArray.get(0);
 JSONOArray array = jsonObj.get("hq");

You will get array and iterate over array for inner elements.

Ramesh Fadatare
  • 561
  • 4
  • 12