<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
public class Person {
private String name;
private int age;
}
import org.json.simple.JSONObject;
public class Main {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", person.getName());
jsonObject.put("age", person.getAge());
String jsonStr = jsonObject.toJSONString();
System.out.println(jsonStr);
}
}
json
{
"age":25
}
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Main {
public static void main(String[] args) {
JSONParser jsonParser = new JSONParser();
try {
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonStr);
String name = (String) jsonObject.get("name");
int age = ((Long) jsonObject.get("age")).intValue();
Person person = new Person(name, age);
System.out.println(person.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}