Parsing JSON string in Java

To parse a JSON string in Java, you can use the JSONObject class from the org.json package. This class provides methods for parsing JSON strings and converting them to JSONObjects and JSONArrays.

Here's an example of how you can parse a JSON string in Java:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";

    JSONObject json = new JSONObject(jsonString);
    String name = json.getString("name");
    int age = json.getInt("age");
    String city = json.getString("city");

    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("City: " + city);
  }
}

This code creates a JSON string and then uses the JSONObject class to parse it. It gets the values of the "name", "age", and "city" fields using the getString and getInt methods, and prints them to the console.

You can also use the getJSONArray method to parse JSON arrays and the getJSONObject method to parse nested JSON objects.

Note that the JSONObject class is part of the org.json library, which is not included in the standard Java library. You will need to include the org.json library in your project in order to use the JSONObject class.