简体   繁体   中英

How to parse JSON value in Java?

I have JSON like this:

{
    "results": [
        {
            "updated": false,
            "notification": false,
            "id": 123456,
            "mwb": {
                "id": 15989,
                "mwb": "59595959",
                "pieces": 0,
                "origin": "RO",
                "destination": "RU",
                "status": "SOLVED",
                "bbs": null
            },

i'm trying to get some values from results (for example id) this way:

String shipmentStatus = shipmentData.getJSONObject("results").getString("id"); 

But it throws a excpetion. How i should to parse this JSON most easily? Thanks for any advice.


Exception error is:

03-13 14:20:04.773: W/System.err(27604): at org.json.JSON.typeMismatch(JSON.java:100)     
03-13 14:20:04.773: W/System.err(27604): at org.json.JSONObject.getJSONObject(JSONObject.java:573) –

Try something in the lines of this:

String shipmentStatus = ((JSONObject)
    ((JSONArray)shipmentData
        .getJSONArray("results"))
            .get(0))
            .getString("id"); 
 JsonObject _obj  = new JsonObject("Your result String");
 JsonArray _jarray = _obj.getjsonarray(results);
 for(int i = 0; i<_jarray.length; i++){
 jsonobject obj = _jarray.getjsonobject(i);
 String _updated = obj.getstring("updated");
  String _notification= obj.getstring("notification");
  String _id= obj.getstring("id");


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM