I make String jsonStr = response.json () to get data from ApiVk. As a result, I get

String jsonStr = "{\"response\":{\"count\":254,\"items\":[{\"last_name\":\"Екимов\",\"id\":325813465,\"first_name\":\"Артём\"},{\"last_name\":\"Виноградов\",\"id\":448209461,\"first_name\":\"Дэнчик\"},{\"last_name\":\"Κонстантинов\",\"id\":444441827,\"first_name\":\"Κонстантин\"}]}}"; 

How do I parse a line to get an array of id values

 JSONObject jsonObj = new JSONObject(jsonStr); name = jsonObj.getString("id");выдает исключение first = jsonObj.getJSONObject("items").getString("id"); 

throws nullpointer exception

    2 answers 2

    You need to deal with the structure. I advise the site https://jsoneditoronline.org

     { "response": { "count": 254, "items": [ { "last_name": "Екимов", "id": 325813465, "first_name": "Артём" }, { "last_name": "Виноградов", "id": 448209461, "first_name": "Дэнчик" }, { "last_name": "Κонстантинов", "id": 444441827, "first_name": "Κонстантин" } ] } } 

    From this it is clear that first you need to refer to the root element of the response , then to the array of items . In the end, you need something like this

     name = jsonObj.getJSONObject("response").getJSONArray("items")[0].getString("id") 

      regex'om can
      here, look: regex101.com
      here is the regex itself (without escaping): {\ s + "last_name": \ s + "? ([^"] +) "?, \ s +" id ": \ s + (\ d +), \ s +" first_name ": \ s + "? ([^"] +) "? \ s +}
      doing the parsing method, as a result you get a class with matches
      last_name - [1]
      id - [2]
      first_name - [3]
      parsing results parsing results