# Parse JSON Strings in Java Using org.json or Gson

> Use the org.json library and JSONObject class to parse JSON strings in Java, with Maven dependency setup and examples for extracting typed values

**URL:** https://sentry.io/answers/how-do-i-parse-json-in-java/

---

## The Problem

You don't know how to parse JSON in Java.

## The Solution

You can easily use the `org.json` library to parse JSON in Java.

First, include the following dependency in your project:

```xml
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20260814</version>
</dependency>
```

Then, you can use the `org.json` library and the `JSONObject` class to parse a JSON string:

```java
import org.json.JSONObject;

class Main {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"Abdul\", \"age\":20}";
        JSONObject jsonObject = new JSONObject(jsonString);

        String name = jsonObject.getString("name");
        int age = jsonObject.getInt("age");

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

In this example, the `JSONObject` class is used to parse the JSON string and turn it into a `JSONObject` object.

Once you have a `JSONObject` object, you can use the `getXXX` methods, like `getString` and `getInt`, to extract data from it.

Depending on your requirements, other libraries like [Gson](https://mvnrepository.com/artifact/com.google.code.gson/gson) are also available for working with JSON in Java.

---

*Source: [sentry.io/answers/how-do-i-parse-json-in-java/](https://sentry.io/answers/how-do-i-parse-json-in-java/)*
