Ambu
Ambu

Reputation: 194

Failed to read JSON file in new IDE

I have an Eclipse project with the following code:

import org.json.*;
import org.json.simple.JSONObject;

import java.io.*;
import java.util.Iterator;

(...)

public static void main( String[] args )
{   
String resourceName = "C:\\Users\\Snail_Sniffer\\Desktop\\books.json";
String jsonData = readFile(resourceName);   
JSONObject jobj = new JSONObject(jsonData);
(...)

It produces no errors and works as intended, but when I reuse the same code in IntelliJ, it produces following errors:

Error:java: constructor JSONObject in class org.json.simple.JSONObject cannot be applied to given types; required: no arguments found: java.lang.String reason: actual and formal argument lists differ in length


Error:java: cannot find symbol symbol: method getString(java.lang.String) location: variable jobj of type org.json.simple.JSONObject

What is causing the issue and how to workaround it?

Upvotes: 1

Views: 1965

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40068

I'm not sure which library you are using in eclipse, but org.json.simple.JSONObject does not have constructor with String argument. It has only no argument constructor

public JSONObject()

If you want to parse json string using org.json.simple library you need JSONParser

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonData);

Upvotes: 1

Related Questions