How to Parse JSON in Java
Java requires external libraries for JSON parsing, unlike JavaScript which has built-in support. The most popular choices are Jackson, Gson, or the simple org.json library.
Using org.json (lightweight):
import org.json.JSONObject;
import org.json.JSONArray;
String jsonString = "{\"name\":\"Claus Witt\",\"ideas\":[\"idea one\", \"idea two\"]}";
JSONObject obj = new JSONObject(jsonString);
System.out.println(obj.getString("name"));
JSONArray ideas = obj.getJSONArray("ideas");
System.out.println(ideas.getString(0));
Using Jackson (more robust):
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonString);
System.out.println(root.get("name").asText());