Stakerrrr
Stakerrrr

Reputation: 23

Parsing JSON array values

I faced with the problem while trying parse JSON array and list all values it has, I have the following JSON format

{
  "sdd": {
        "token":"1",
        "details":[{
              "type":"SOME_TYPE",
              "l":,
              "expiration_date":"12\/2020",
              "default":true,
              "expired":false,
              "token":"1"
         }]
   }
 } 

JSON output I have

public void onResponse(JSONObject response) {
    try {
        JSONArray ja = response.getJSONArray("ssd");
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jobj = ja.getJSONObject(i);
            Log.e(TAG, "response" + jobj.getString("token"));
            Log.e(TAG, "response" + jobj.getString("details"));
        }
    } catch(Exception e) { e.printStackTrace(); }
}

and in the log cat I getting org.json.JSONException: No value for ssd this output

Upvotes: 2

Views: 102

Answers (3)

hamid
hamid

Reputation: 191

hi you must json file isn't create is create :

{ "sdd":{ "token":"1", "details":[ { "type":"SOME_TYPE", "expiration_date":"12/2020", "default":true, "expired":false, "token":"1" } ] } }

after you can get data from code :

public void onResponse(JSONObject response) {
    try {
        JSONObject ssd = response.getJSONObject("ssd");
        JSONArray details = ssd.getJSONArray("details");
        for (int i = 0; i < details.length(); i++) {
            JSONObject obj = details.getJSONObject(i);
            Log.e(TAG, "response" + obj.getString("type"));
            Log.e(TAG, "response" + obj.getString("expiration_date"));
            Log.e(TAG, "response" + obj.getBoolean("default"));
            Log.e(TAG, "response" + obj.getBoolean("expired"));
            Log.e(TAG, "response" + obj.getString("details"));

        }
    }catch (Exception e){e.printStackTrace();}
}

Upvotes: -1

Axbor Axrorov
Axbor Axrorov

Reputation: 2806

You have typo. Not ssd but sdd. And also sdd is not array, but object. So you must write like:

JSONObject jb = response.getJSONObject("sdd");

Full parsing code will be like:

public void onResponse(JSONObject response) {
    try {
        JSONObject sdd = response.getJSONObject("sdd");
        JSONArray details = sdd.getJSONArray("details");
        for (int i = 0; i < details.length(); i++) {
            JSONObject jobj = details.getJSONObject(i);
            Log.e(TAG, "response-type:" + jobj.getString("type"));
            Log.e(TAG, "response-token:" + jobj.getString("token"));
            Log.e(TAG, "response-expiration_date:" + jobj.getString("expiration_date"));
            Log.e(TAG, "response-default:" + jobj.getBoolean("default"));
            Log.e(TAG, "response-expired:" + jobj.getBoolean("expired"));
        }
    } catch(Exception e) { e.printStackTrace(); }
}

Also, let me suggest you to use gson this library will help you deserialize your json representations.

Upvotes: 4

sirnino
sirnino

Reputation: 427

ssd is an object. You can get the array as follows:

JSONObject jo = response.getJSONObject("sdd");
JSONArray ja = jo.getJSONArray("details");

Upvotes: 1

Related Questions