Home >Java >javaTutorial >How to Parse a JSON Object and Store Specific Keys in an ArrayList Using Java?
Many JSON parsing frameworks are available in Java, allowing developers to work with JSON data conveniently. This question revolves around parsing a specific JSON object structure and storing interest keys in an array list.
To achieve this using the org.json library:
import org.json.JSONObject; import org.json.JSONArray; import java.util.ArrayList; JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs}, {interestKey:Cats}]}"); List<String> list = new ArrayList<>(); JSONArray array = obj.getJSONArray("interests"); for(int i = 0 ; i < array.length() ; i++){ list.add(array.getJSONObject(i).getString("interestKey")); }
This approach will effectively store all the interestKeys in an array list, providing easy access and manipulation within the Java code.
The above is the detailed content of How to Parse a JSON Object and Store Specific Keys in an ArrayList Using Java?. For more information, please follow other related articles on the PHP Chinese website!