In the format of the complex JSON data, the JSON data is often nested, so that the value will be slightly more complex than the previous value, but as long as the idea is clear, in fact, the method is the same. Just like if Else statement, if the IF in the set if,if if, write the specification is OK, if the code format is not standardized, then we must also look at the trouble. So, for JSON nesting, just remember the symbol ":" is the key, after the symbol is a value, curly braces in pairs to find, a layer of peel off, it is clear. As an example, the following:
{ "ResultCode": "200", "Reason": "Successful Return", "Result":{ "Company": "Shun Fung", "com": "SF", "No": "575677355677", "List":[ { "datetime": "2013-06-25 10:44:05", "Remark": "Received", "Zone": "Taizhou" }, { "datetime": "2013-06-25 11:05:21", "Remark": "Express in Taizhou, ready to send to the next station Taizhou distribution Center", "Zone": "Taizhou" } ], "Status": 1 }, "Error_code": 0}
How to get
1 PackageCom.json;2 3 ImportNet.sf.json.JSONArray;4 ImportNet.sf.json.JSONObject;5 6 Public classJsonobjectandjsonarraydemo {7 Public Static voidMain (string[] args) {8 //Complex JSON data9String jsonstr = "{\" resultcode\ ": \" 200\ ", \" reason\ ": \" successful return \ ", \" result\ ": {\" company\ ": \" Shun Fung \ ", \" com\ ": \" sf\ ","Ten+ "\" no\ ": \" 575677355677\ ", \" list\ ": [{\" datetime\ ": \" 2013-06-25 10:44:05\ ", \" Remark\ ": \" received \ ", \" zone\ ": \" Taizhou \ "}, " One+ "{\" datetime\ ": \" 2013-06-25 11:05:21\ ", \" Remark\ ": \" Express in Taizhou, ready to send to the next station Taizhou distribution center \ "," zone\ ": \" Taizhou \ "}],\" Status\ ": 1}," A+ "\" Error_code\ ": 0}"; -Jsonobject json = Jsonobject.fromobject (JSONSTR);//get the entire JSON string -System.out.println ("ResultCode:" +json.getstring ("ResultCode"));//get value:200 based on key theSystem.out.println ("Reason:" +json.getstring ("Reason"));//get value based on key: successful return - - //When the result is encountered, it is also treated as a whole string -System.out.println ("Company:" +jsonobject.fromobject (json.getstring ("result")). GetString ("Company"))); +System.out.println ("com:" +jsonobject.fromobject (json.getstring ("result")). GetString ("com")); -System.out.println ("No:" +jsonobject.fromobject (json.getstring ("result")). GetString ("No")); + A //when nested again, the list at this time is a jsonarray, so it needs to be treated as an array, in fact, a string at //get (i) the following is the main, the inside of the thing, is actually a JSON form of data, you can not look at how to nest outside, according to the truth only as a single JSON string processing can -System.out.println ("list (0). DateTime:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (0)). GetString ("datetime").)); - -System.out.println ("list (0). Remark:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (0)). GetString ("Remark")); - -System.out.println ("list (0). Zone:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (0)). getString ("zone")); in -System.out.println ("list (1). DateTime:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (1)). GetString ("datetime").)); to +System.out.println ("list (1). Remark:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (1)). GetString ("Remark")); - theSystem.out.println ("list (1). Zone:" +jsonobject.fromobject (Jsonarray.fromobject (Jsonobject.fromobject ( Json.getstring ("result")). GetString ("list")). Get (1)). getString ("zone")); * $System.out.println ("Status:" +jsonobject.fromobject (json.getstring ("result")). GetString ("status"));Panax Notoginseng -System.out.println ("Reason:" +json.getstring ("Error_code")); the } +}
Excerpt from: JSON4: Nested jsonobject and Jsonarray values
2017-12-07 09:57:13
Nested jsonobject and Jsonarray values---nested jsonarray in JSON