sof question
sof question

Reputation: 93

How to get value of jsonObject value in android?

I am building an android application and I am new to json. I am fetching below josn formate -

{
    "contact"[
        {
            "key1": "hey1",
            "key2": [
                {
                    "key3": "hey2"
                }
            ]
        }
    ]
}

I am using below code to fetch key1 value. Now problem I am facing is how to fetch key3 value -

jsonString = http.makeServiceCall(url, ServiceHandler.GET, null);
if (jsonString != null) {
    try {
        JSONObject jsonObj = new JSONObject(jsonString);

        // Getting JSON Array node
        questions = jsonObj.getJSONArray(TAG_CONTACTS);
        for (int i = 0; i < questions.length(); i++) {
            temp_obj = questions.getJSONObject(i);
            key1Array.add(temp_obj.getString("key1").toString());
        }
    } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
}

Please help me

Upvotes: 2

Views: 208

Answers (3)

Robust
Robust

Reputation: 2485

If you want to use Gson to parse your json data. Let try it:

First of all, you must modify your json like this:

{
    "contact":[
        {
            "key1": "hey1",
            "key2": [
                {
                    "key3": "hey2"
                }
            ]
        }
    ]
}

Second add Gson to your libs and sync build.gradle: download here extract it, and copy/past gson-2.2.4.gson to libs folder.

Third Create some class:

FullContents.java:

public class FullContents {
    private List<ObjectKey> contact;

    public List<ObjectKey> getContact() {
        return contact;
    }

    public void setContact(List<ObjectKey> contact) {
        this.contact = contact;
    }
}

ObjectKey.java:

public class ObjectKey {
    private String key1;

    private List<ObjectKey3> key2;

    public List<ObjectKey3> getKey2() {
        return key2;
    }

    public void setKey2(List<ObjectKey3> key2) {
        this.key2 = key2;
    }

    public String getKey1(){
        return key1;
    }
    public void setKey1(String key1){
        this.key1 = key1;
    }
}

ObjectKey3.java:

public class ObjectKey3 {
    private String key3;
    public String getKey3(){
        return key3;
    }
    public void setKey3(String key3){
        this.key3 = key3;
    }
}

And Finally, get data from url:

 private class ParseByGson extends AsyncTask<String,Void,FullContents> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected FullContents doInBackground(String... params) {
        FullContents fullContents = null;
        try {
            URL url=new URL(params[0]);
            InputStreamReader reader=new InputStreamReader(url.openStream(),"UTF-8");
            fullContents=new Gson().fromJson(reader,FullContents.class);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fullContents;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(FullContents results) {
        super.onPostExecute(results);
        ObjectKey objectKey = results.getContact().get(0);
        Log.e(">>",objectKey.getKey1()+"--");
    }
}

you can put below code to onCreate:

ParseByGson parseByGson = new ParseByGson();
parseByGson.execute(urlStringHere);

Update: Explain

enter image description here

Upvotes: 1

Lennon Spirlandelli
Lennon Spirlandelli

Reputation: 3193

Adapt the following code to what you are coding

for (int i = 0; i < questions.length(); i++) {
   temp_obj = questions.getJSONObject(i);

   key1Array.add(temp_obj.getString("key1"));

   JSONObject temp_objKey2 = temp_obj.getJSONObject("key2");

   Key2Object key2Object = new Key2Object();

   key2Object.add(temp_objKey2.getString("key3"));

   key1Array.add(key2Object);

}

Upvotes: 0

mend3
mend3

Reputation: 516

1st of all: your json appears to be not valid (missing ':' after "content");

After reviewing thins:

You can use the named getters to retrieve many types of results (object, int, string, etc);

JSONObject contact = jsonObj.getJSONObject("contact"); // {"key1":"hey1","key2":[{"key3":"hey2"}]}

or

String key1 = jsonObj.getString("key1"); // hey1

To retrieve key3, you should use:

JSONObject contact = jsonObj.getJSONObject("contact");
JSONObject key2 = contact.getJSONObject("key2");
String key3 = key2.getString("key3");

Upvotes: 0

Related Questions