1

I am trying to read a json file and convert it to the jsonObject and when I searched on how to do it, I came across the method to user

JSONParser parser= new JSONParse();

But the version of org.json I am using in the code is "20180803". It does not contain JSONParser. Has it been removed from the org.json package? If so what is the new class or method that I could use to read a json file and convert it to a json object.

My dependency is given below :

       <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>
Mano Kugan
  • 207
  • 6
  • 23

4 Answers4

2

Hi you can use simple JSON. You just need to add in your pom.xml file:

<dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
</dependency>

Sample code

public static JSONObject convertJsonStingToJson(String jsonString) {
    JSONParser parser = new JSONParser();
    return  json = (JSONObject) parser.parse(jsonString);
}
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
1

org.json library has very simple API which does not have JSONParser but has JSONTokener. We can construct JSONObject or JSONArray directly from String:

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonApp {

    public static void main(String[] args) {
        // JSON Object
        String object = "{\"p1\":\"v1\", \"p2\":2}";
        JSONObject jsonObject = new JSONObject(object);
        System.out.println(jsonObject);

        // JSON Array
        String array = "[{\"p1\":\"v1\", \"p2\":2}]";
        JSONArray jsonArray = new JSONArray(array);
        System.out.println(jsonArray);
    }
}

Above code prints:

{"p1":"v1", "p2":2}
[{"p1":"v1","p2":2}]

You need to notice that it depends from JSON payload which class to use: if JSON starts from { use JSONObject, if from [ - use JSONArray. In other case JSON payload is invalid.

As it mentioned in other answers, if you can you should definitely use Jackson or Gson

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
0

The short answer to your question is: No, it was not removed, because it never existed.

I think you are mentioning a library, and trying to use another one. Anyway, if you really want to use org.json, you can find how here

JSONObject jsonObject = new JSONObject(instanceOfClass1);
String myJson = jsonObject.toString();
finx
  • 1,293
  • 9
  • 9
0

Add following dependency in build file

//json processing
implementation("com.fasterxml.jackson.core:jackson-core:2.9.8")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.9.8")
implementation("com.fasterxml.jackson.core:jackson-databind:2.9.8")

a.json file

{
  "a": "b"
}

code:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.FileInputStream;
import java.io.InputStream;


public class App {

    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        InputStream input = new FileInputStream("a.json");
        JsonNode obj =  objectMapper.readTree(input);
        System.out.println(obj.get("a")); // "b"
    }
}
dkb
  • 4,389
  • 4
  • 36
  • 54