0

I want to get data from the ApiVk method response.responseString

String jsonStr1 =response.responseString;
JSONArray arr = new JSONArray(jsonStr1);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("id");

And i'm getting NPE How do I parse the string to get an array of values "id"

String jsonStr1 = "{\"response\":{\"count\":254,\"items\":[{\"last_name\":\"Екимов\",\"id\":325813465,\"first_name\":\"Артём\"},{\"last_name\":\"Виноградов\",\"id\":448209461,\"first_name\":\"Дэнчик\"},{\"last_name\":\"Κонстантинов\",\"id\":444441827,\"first_name\":\"Κонстантин\"}]}}";
1
  • 1
    JSON response is not an array. json["response"]["items"] is an array. Commented Nov 25, 2018 at 16:01

1 Answer 1

1

For this json:

{
  "response": {
      "count": 254,
      "items": [{
          "last_name": "Екимов",
          "id": 23892340,
          "first_name": "Артём"
      }, {
          "last_name": "Екимов",
          "id":  23892381,
          "first_name": "Артём"
      }, {
          "last_name": "Екимов",
          "id": 23828492,
          "first_name": "Артём"
      }]
  }
}

The analysis to get the id is as follows:

try {
        JSONObject jsonresponse = jsonResponse.getJSONObject("response");
        JSONArray getJsonArray = jsonresponse.getJSONArray("items");
        for(int i = 0; i<getJsonArray.length();i++){
            JSONObject jsonAux= (JSONObject) jsonArrayItems.get(i);
            String id = jsonAux.getString("id");
            Log.e("ID = ",id);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

I hope it helps.

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

Comments

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.