Parsing JSON Object in Java
To parse a JSON object in Java, you can use the org.json
library. This library provides a simple and easy-to-use set of classes to parse and generate JSON in Java.
Here is an example of how you can use the org.json
library to parse a JSON object in Java:
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
// Create a JSON object from a string
String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject obj = new JSONObject(jsonString);
// Get values from the object
String name = obj.getString("name");
int age = obj.getInt("age");
String city = obj.getString("city");
// Print the values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
This code creates a JSON object from a string and uses the getString()
, getInt()
, and getString()
methods to retrieve