import org.codehaus.jettison.json.JSONObject;
public class JsonConverter {
public static void main(String[] args) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John Doe");
jsonObject.put("age", 30);
jsonObject.put("city", "New York");
String jsonString = jsonObject.toString();
System.out.println(jsonString);
}
}
json
{"city":"New York","name":"John Doe","age":30}
import org.codehaus.jettison.json.JSONObject;
public class JsonConverter {
public static void main(String[] args) throws Exception {
String jsonString = "{\"name\":\"John Doe\",\"age\":30,\"city\":\"New York\"}";
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);
}
}
Name: John Doe
Age: 30
City: New York
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.0</version>
</dependency>