Let's assume you have a class called Person with just name as an attribute.
For example:
private class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
Do you want to learn Java end to end! Here's the right video for you:
For the serialization/de-serialization of objects, you can do this:
Gson g = new Gson();
Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John
System.out.println(g.toJson(person)); // {"name":"John"}
If you want to get a single attribute out you can do it easily with the Google library as well, do like this:
JsonObject jsonObject = new JsonParser().parse("{\"name\": \"John\"}").getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString());
But, if you don't need object de-serialization but to simply get an attribute, you can try org.json
JSONObject obj = new JSONObject("{\"name\": \"John\"}");
System.out.println(obj.getString("name"));
ObjectMapper mapper = new ObjectMapper();
Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);
System.out.println(user.name);