<dependencies> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.4.0</version> </dependency> </dependencies> import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONException; public class JSONParser { public static void main(String[] args) { String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}"; try { JSONObject jsonObject = new JSONObject(jsonString); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("City: " + city); } catch (JSONException e) { e.printStackTrace(); } } } import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONException; public class JSONGenerator { public static void main(String[] args) { String name = "John"; int age = 30; String city = "New York"; try { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", name); jsonObject.put("age", age); jsonObject.put("city", city); String jsonString = jsonObject.toString(); System.out.println("JSON String: " + jsonString); } catch (JSONException e) { e.printStackTrace(); } } }


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