我想傳遞一個JSON響應字符串作爲參數傳遞給一個JSONObject由雅虎http://developer.yahoo.com/java/howto-parseRestJava.html在下面給出的代碼所示,這是爲了讓雅虎的搜索結果:解析來自Yahoo BOSS的Json響應。錯誤
/**
* 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("--");
}
}
}
添加所需的後爲驗證碼,包括所需的罐子,我得到以下錯誤:
JSONObject["ResultSet"] not found. at 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"] not found. at 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解析。我可能在某個點或另一個點有誤解。請澄清。