Java JSON构造函数显示为未定义

Java JSON构造函数显示为未定义,java,json,url,connection,Java,Json,Url,Connection,JSONObject有一个可用的构造函数JSONObject(字符串s)可用,但是eclipse告诉我它是未定义的 我的代码如下: URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q=barack%20obama&userip=INSERT-USER-IP"); URLConnection connection = url.openConnection()

JSONObject
有一个可用的构造函数
JSONObject(字符串s)
可用,但是eclipse告诉我它是未定义的

我的代码如下:

URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q=barack%20obama&userip=INSERT-USER-IP");

URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",  "http://google.com");

String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while((line = reader.readLine()) != null) {
   builder.append(line);
}

JSONObject json = new JSONObject(builder.toString()); //Error, undefined

我已经三次检查了我的所有库是否都是最新的、最稳定的,以及是否正确实现了(除了我的构建路径)。

使用类似以下代码:

static String jsonData;
File json = new File("JSONFile.json");
FileReader fr=new FileReader(json);
BufferedReader br=new BufferedReader(fr);
StringBuilder sb=  new StringBuilder();
while((jsonData = br.readLine())!=null)
{
    sb.append(jsonData);
}
jsonData = sb.toString();
br.close();

JSONObject jsonObject = new JSONObject(jsonData);
System.out.println(jsonObject.toString(4));

尝试使用此代码,因为我得到了此代码的结果。

使用类似以下代码的内容:

static String jsonData;
File json = new File("JSONFile.json");
FileReader fr=new FileReader(json);
BufferedReader br=new BufferedReader(fr);
StringBuilder sb=  new StringBuilder();
while((jsonData = br.readLine())!=null)
{
    sb.append(jsonData);
}
jsonData = sb.toString();
br.close();

JSONObject jsonObject = new JSONObject(jsonData);
System.out.println(jsonObject.toString(4));

尝试使用此代码,因为我得到了此结果。

是否将其添加到构建路径

现解释如下:


是否将其添加到构建路径

现解释如下:


我想这应该适合你了

URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q=barack%20obama&userip=INSERT-USER-IP");

URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",  "http://google.com");

String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while((line = reader.readLine()) != null) {
   builder.append(line);
}

JSONObject json = new JSONObject(line);
System.out.println(json.toString(4));

你把生成器放在JSONObject参数中,这是个错误……我希望它能起作用……最好的

我想现在应该对你起作用了

URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q=barack%20obama&userip=INSERT-USER-IP");

URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",  "http://google.com");

String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while((line = reader.readLine()) != null) {
   builder.append(line);
}

JSONObject json = new JSONObject(line);
System.out.println(json.toString(4));

您将生成器放在JSONObject参数中,这是一个错误……我希望这会起作用……在我尝试实例化JSONObject时,Eclipse仍然给我“构造函数JSONObject(字符串)未定义”。Eclipse仍然给我“构造函数JSONObject(字符串)未定义”当我尝试实例化jsonObject时,我添加了一个不同的库作为库,所以我删除了它,并将其替换为答案中的库。字符串是——我可以实例化jsonObject,但前提是我通过
org.json.jsonObject jsonObject=new org.json.jsonObject(jsonData)调用它没关系-慢夜,我导入了org.json.*;它修复了它。我添加了一个不同的库作为库,所以我删除了它,并用那个答案中的一个替换它。字符串是——我可以实例化jsonObject,但前提是我通过
org.json.jsonObject jsonObject=new org.json.jsonObject(jsonData)调用它没关系-慢夜,我导入了org.json.*;它把它修好了。