JSONObject parse dictionary objects

10,496

Solution 1

Instead of :

String result = json.getString("Result");

use

if(json.get("Result") instanceof JSONObject){
    JSONObject object = (JSONObject) json.get("Result");
    //do what you want with JSONObject
    String ob = object.get("0B");

}

If you want to store it some way you can put it to Map or create object if always it is same data

Solution 2

You can use some libraries such as Gson (Google) or Moshi (Square) Those libraries allows you to declare your model as a plain java class (commonly called POJOS) annotated in some way that this libraries bind your properties in the JSON to your java properties.

In your case:

JSON:

{
"Status":0,
"Message":"",
"Result":{"0B":"S.C. Blue Air","0Y":"FlyYeti","1X":"Branson Air"}
}

MODEL:

public class MyCallResponse {
    @SerializedName("Status")
    int status;
    @SerializedName("Message")
    String message;
    @SerializedName("Result")
    Result result;
}

public class Result {
    @SerializedName("0B")
    String b;
    @SerializedName("0Y")
    String y;
    @SerializedName("0X")
    String x;
}

In this case, with Gson you can do:

MyCallResponse response = new Gson().fromJson(json, MyCallResponse.class);
Log.i("Response b", response.result.b);

Look at the documentation for more information about both libraries.

Solution 3

Try this

String base = ""; //Your json string;
JSONObject json = new JSONObject(base);
JSONOBject resultJson = json.getJSONObject("Result");

// Get all json keys "OB", "OY", "1X" etc in Result, so that we can get values against each key.
Set<Map.Entry<String, JsonElement>> entrySet = resultJson.entrySet();

Iterator iterator = entrySet.iterator();

for (int j = 0; j < entrySet.size(); j++) {
    String key = null; //key = "OB", "OY", "1X" etc
    try {
        Map.Entry entry = (Map.Entry) iterator.next ();
        key = entry.getKey ().toString ();
      //key = "OB", "OY", "1X" etc
    }
    catch (NoSuchElementException e) {
        e.printStackTrace ();
    }
    if (!TextUtils.isEmpty (key)) {

        Log.d ("JSON_KEY", key);
        String value = resultJson.getString(key);
        //for key = "0B", value = "S.C. Blue Air"
        //for key = "0Y", value = "FlyYeti"
        //for key = "1X", value = "Branson Air"
    }
}

It works with any array with dynamic json key.

Don't forget to accept the answer & upvote if it works.

Solution 4

try this :

JSONObject json = new JSONObject(response);
JSONObject resultObj = json.getJSONObject("Result");
String OB = resultObj.getString("OB");
Share:
10,496

Related videos on Youtube

bkm
Author by

bkm

Updated on September 15, 2022

Comments

  • bkm
    bkm over 1 year

    JSON values that I get from server:

    {
    "Status":0,
    "Message":"",
    "Result":{"0B":"S.C. Blue Air","0Y":"FlyYeti","1X":"Branson Air"}
    }
    

    Getting the result as 'response' after connection and I am able to show my JSON string results on the screen.

     JSONObject json = new JSONObject(response);
     String status = json.getString("Status");
     String message = json.getString("Message");
     String result = json.getString("Result");
     responseView.setText("Status" + status+ "Message" + message" + Result" + result);
    

    I am okay the results of "Status" and "Message" but not with "Result" because want to separate "Result" objects as and able use each of them as objects.

    For example: When I type OB in my app, I will get the result S.C. Blue Air

  • bkm
    bkm almost 8 years
    my result is a object. But how can I access OB and S.C. Blue Air objects separately. Should I put the object in a hashmap?