次の特定のコードでYahoo http://developer.yahoo.com/java/howto-parseRestJava.htmlで示されているように、Json応答文字列を引数としてjsonObjectに渡そうとしてい ます。これはyahoo検索を取得するためです結果:
/**
* 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) で org.json.JSONObject.getJSONObject(JSONObject.java:516) で SignPostTest.parseResponse(SignPostTest.java:187) で 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":
応答とオブジェクト名を見ると、Yahoo が応答にいくつかの変更を加えたようで、コードの次の 2 行を次のように変更しました。
// 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) で org.json.JSONObject.getJSONArray(JSONObject.java:498) で SignPostTest.parseResponse(SignPostTest.java:185) で SignPostTest.main(SignPostTest.java: 222)
どこに問題があるのかわからない。json 解析を行うのはこれが初めてです。ところどころ誤解があるかもしれません。どうか明らかにしてください。