2

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

3
  • its not a valid json. Please update your right Json. Commented Dec 24, 2018 at 17:59
  • your json file is error(line 6), check him ! Commented Dec 24, 2018 at 18:17
  • You could use gson to automatically parse json data. Commented Dec 24, 2018 at 18:48

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hold on let me try it
@Stakerrrr if you solve your problem with my answer, you must mark it as Accepted.
1

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

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

2 Comments

Note also that the JSON you are trying to parse is not valid.
Before you had "no value for ssd" and now "no value for token". Right? Try to fix the JSON and update the request, please.
-1

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();}
}

3 Comments

there is typo, it will gave same error, in json representation wroted sdd but in code wroted like ssd
@AxborAxrorov my mistake sorry, I can tick your answer as correct if you want
@Stakerrrr oh ! is so bad , you first test my code and solved your problem ,So the best answer is my answer . please tick my answer thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.