Json looks like:

{ "cover":"blabla.jpg", "content":[ { "article":"article_text", "document":"document_text" } ] } 

I can get the value of the "cover" field easily

 JSONObject json = new JsonObject(jsonStr); //jsonStr - мой json в видео строки json.get("cover"); 

But how to get to the value of "article"?

  • Well, apparently json.getJsonArray ("content"). get (0). getString ("article"); - pavel
  • @pavel tried this; error, .get(0) returns a string, it does not have a method .getString() - Artyom Chebotaryov
  • 2
    and if instead of get getJSONObject (0) write? - pavel
  • @pavel thanks, it came out! - Artyom Chebotaryov

1 answer 1

To get an array of objects, there is a getJSONArray(название) method. To get the object getJSONObject(номер) , the number of elements length() .

Together

 json.getJSONArray("content").getJSONObject(0).getString("article"); 
  • thanks, my wrapper was written here and copied it by accident) - pavel
  • one
    If the json structure is known in advance, it is better to make a class under this structure and deserialize json into this class. Such code is much easier to read, more convenient to work with and easier to refactor. - Mikhail Kopylov