<dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.4.0</version> </dependency> json { "name": "John Doe", "age": 25, "email": "johndoe@example.com" } import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class JsonParser { public static void main(String[] args) { String jsonStr = "{\"name\":\"John Doe\",\"age\":25,\"email\":\"johndoe@example.com\"}"; try { JSONObject jsonObject = new JSONObject(jsonStr); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String email = jsonObject.getString("email"); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Email: " + email); } catch (JSONException e) { e.printStackTrace(); } } } import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class JsonSerializer { public static void main(String[] args) { try { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "John Doe"); jsonObject.put("age", 25); jsonObject.put("email", "johndoe@example.com"); String jsonString = jsonObject.toString(); System.out.println(jsonString); } catch (JSONException e) { e.printStackTrace(); } } }


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