There is a line with JSON'om:

{"response":{"count":544495,"items":[358358919,358357662,358352453,358351002,358350593,358345630,358338466,358337444,358332410,358322758]}} 

I'm trying to get count in an int'овую variable, and items in JSONArray (to translate then into ArrayList<String> ):

 JSONObject jsonObject = new JSONObject(jsonString); int count = jsonObject.getInt("count"); JSONArray jsonArray = jsonObject.getJSONArray("items"); 

But, as a result, I get errors:

org.json.JSONException: No value for count

org.json.JSONException: No value for items

Why does an error pop up if, in the sense, there are data fields? And what fields do I need to request in order to get the result I need?

  • @YuriSPb, Error:(67, 84) error: incompatible types: String cannot be converted to int . - user189127

1 answer 1

 String json = "{\"response\":{\"count\":544495,\"items\":[358358919,358357662,358352453,358351002,358350593,358345630,358338466,358337444,358332410,358322758]}}"; try { JSONObject rootJSON = new JSONObject(new JSONTokener(json)); JSONObject response = rootJSON.getJSONObject("response");//вы забыли эту строку Log.d("TAG", String.valueOf(response.getInt("count"))); JSONArray items = response.getJSONArray("items"); for (int i = 0; i < items.length(); i++) { int item = items.getInt(i); Log.d("TAG", String.valueOf(item)); } } catch (JSONException e) { e.printStackTrace(); } 
  • And what does new JSONToken do? - user189127
  • @ bukashka101, for better compatibility - katso
  • And ... Well then, just in case, I will add (although it worked for me without it :)). - user189127