我试图将Json响应字符串作为参数传递给jsonObject,如下面给出的代码中的Yahoo http://developer.yahoo.com/java/howto-parseRestJava.html所示,这是为了获得雅虎搜索结果:
/**
* ParseYahooSearchResultsJSON.java
* This example shows how to parse Yahoo! Web Service search results returned in JSON format.
*
* @author Daniel Jones www.danieljones.org
*/
import java.io.*;
import org.json.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
public class ParseYahooSearchResultsJSON {
public static void main(String[] args) throws Exception {
String request = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=umbrella&results=10&output=json";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(request);
// Send GET request
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
InputStream rstream = null;
// Get the response body
rstream = method.getResponseBodyAsStream();
// Process the response from Yahoo! Web Services
BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
String jsonString = "";
String line;
while ((line = br.readLine()) != null) {
jsonString += line;
}
br.close();
// Construct a JSONObject from a source JSON text string.
// A JSONObject is an unordered collection of name/value pairs. Its external
// form is a string wrapped in curly braces with colons between the names
// and values, and commas between the values and names.
JSONObject jo = new JSONObject(jsonString);
// A JSONArray is an ordered sequence of values. Its external form is a
// string wrapped in square brackets with commas between the values.
JSONArray ja;
// Get the JSONObject value associated with the search result key.
jo = jo.getJSONObject("ResultSet");
//System.out.println(jo.toString());
// Get the JSONArray value associated with the Result key
ja = jo.getJSONArray("Result");
// Get the number of search results in this set
int resultCount = ja.length();
// Loop over each result and print the title, summary, and URL
for (int i = 0; i < resultCount; i++)
{
JSONObject resultObject = ja.getJSONObject(i);
System.out.println(resultObject.get("Title"));
System.out.println(resultObject.get("Summary"));
System.out.println(resultObject.get("Url"));
System.out.println("--");
}
}
}
添加所需的身份验证代码并包含所需的jar后,我收到以下错误:
找不到JSONObject [“ResultSet”]。在 org.json.JSONObject.get(JSONObject.java:422)at org.json.JSONObject.getJSONObject(JSONObject.java:516)at SignPostTest.parseResponse(SignPostTest.java:187)at SignPostTest.main(SignPostTest.java:222)
这是Json响应字符串的开头:
{"bossresponse":{"responsecode":"200","web":{"start":"0","count":"50","totalresults":"36800","results":[{"date": "","clickurl":"http:\/\/uk.news.yahoo.com\/apple\/","url":"http:\/\/uk.news.yahoo.com\/apple\/","dispurl":"uk.news.yahoo.com\/apple","title":"Latest Apple news | headlines – Yahoo! News UK","abstract":"Get the latest Apple news on Yahoo! News UK. Find in-depth commentary on Apple in our full coverage news section."},{"date": "","clickurl":"http:\/\/answers.yahoo.com\/question\/index?qid=20080113074727AAuSMGy","url":"http:\/\/answers.yahoo.com\/question\/index?qid=20080113074727AAuSMGy","dispurl":"answers.yahoo.com\/question\/index?qid=20080113074727AAuSMGy","title":"JailBreak <b>Ipod? - Yahoo<\/b>! Answers","abstract":"Best Answer: Jailbreaking Guide ok jailbreaking is when you download the AppSnapp (found at www.jailbreakme.com) application to your iPod touch after first ..."},{"date":
查看响应和对象名称,似乎雅虎在响应中做了一些更改,并且我将以下两行代码更改为:
// Get the JSONObject value associated with the search result key.
jo = jo.getJSONObject("bossresponse");
// Get the JSONArray value associated with the Result key
ja = jo.getJSONArray("results");
我仍然收到错误:
org.json.JSONException:找不到JSONObject [“results”]。在 org.json.JSONObject.get(JSONObject.java:422)at org.json.JSONObject.getJSONArray(JSONObject.java:498)at SignPostTest.parseResponse(SignPostTest.java:185)at SignPostTest.main(SignPostTest.java:222)
我不知道问题出在哪里。这是我第一次进行json解析。我可能会在某种程度上产生误解。请澄清一下。
答案 0 :(得分:0)
上面提到的yahoo URL正在发送错误,因为json响应在描述
中说明&#34;该服务已关闭。有关详细信息,请参阅弃用服务博客文章http://developer.yahoo.com/blogs/ydn/posts/2010/08/api_updates_and_changes&#34;
这就是您无法访问JSON Response的预期属性的原因。