To deserialize a JSON object in Java, you can use the org.json
library. Here's an example of how to do this:
import org.json.JSONObject; import org.json.JSONException; // Sample JSON object String jsonObjectString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"; try { JSONObject jsonObject = new JSONObject(jsonObjectString); String name = jsonObject.getString("name"); int age = jsonObject.getInt("age"); String city = jsonObject.getString("city"); System.out.println("Name: " + name + ", Age: " + age + ", City: " + city); } catch (JSONException e) { e.printStackTrace(); }
This will output the following:
Name: John, Age: 30, City: New York
You can also use other JSON parsing libraries, such as Gson or Hymanson, to parse JSON objects in Java.