JSONObject
有一个可用的构造函数JSONObject(String 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
我已经三次检查我的所有库都是最新且最稳定的,并且正确实现(除了我的构建路径)。
答案 0 :(得分:1)
使用类似代码:
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));
尝试使用此代码,因为我得到了我的结果。
答案 1 :(得分:1)
答案 2 :(得分:1)
我认为这应该对你有用.....
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参数中确实错了......我希望这应该工作.....所有最好的