String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); Person person = new Person("John", 30, "New York"); JSONObject jsonObject = new JSONObject(person); String json = jsonObject.toString(); public class Person { @JsonProperty("name") private String name; @JsonProperty("age") private int age; @JsonProperty("city") private String city; } String json = "{\"name\":\"John\", \"age\":30, \"address\":{\"street\":\"123 Main St\", \"city\":\"New York\"}, \"hobbies\":[\"reading\", \"traveling\"]}"; JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); JSONObject address = jsonObject.getJSONObject("address"); String street = address.getString("street"); String city = address.getString("city"); JSONArray hobbies = jsonObject.getJSONArray("hobbies"); for (int i = 0; i < hobbies.length(); i++) { String hobby = hobbies.getString(i); }


上一篇:
下一篇:
切换中文