Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject parse dictionary objects

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

like image 279
bkm Avatar asked Apr 26 '16 17:04

bkm


4 Answers

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

like image 95
mariusz2108 Avatar answered Nov 17 '22 11:11

mariusz2108


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.

like image 28
droidpl Avatar answered Nov 17 '22 11:11

droidpl


try this :

JSONObject json = new JSONObject(response);
JSONObject resultObj = json.getJSONObject("Result");
String OB = resultObj.getString("OB");
like image 1
Milos Lulic Avatar answered Nov 17 '22 11:11

Milos Lulic


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.

like image 1
PEHLAJ Avatar answered Nov 17 '22 10:11

PEHLAJ