Parsing JSON Object in Java
This question seeks to parse a JSON object with a nested array of interest keys, and store the keys in an ArrayList. The sample JSON object provided is as follows:
member = "{interests : [{interestKey:Dogs}, {interestKey:Cats}]}";
To parse this JSON object in Java using the org.json library, follow these steps:
JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}");
JSONArray array = obj.getJSONArray("interests");
List<String> list = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { list.add(array.getJSONObject(i).getString("interestKey")); }
This approach will store the interest keys in an ArrayList. Note that the org.json library is an external dependency that may need to be added to your project before using it.
The above is the detailed content of How to Parse a Nested JSON Array and Store Keys in an ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!