Parsing String Value from Json Object Android

310 Views Asked by At

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

There are 1 best solutions below

0
On

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.