0

我正在尝试将 Json 响应字符串作为参数传递给 jsonObject,如 Yahoo http://developer.yahoo.com/java/howto-parseRestJava.html 在以下给定代码中所示,这是为了让 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":

查看响应和对象名称,雅虎似乎在响应中做了一些更改,我将以下两行代码更改为:

// 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解析。我可能在某一点或另一个方面有误解。请说清楚。

4

1 回答 1

0

上面提到的 yahoo URL 将错误作为 json 响应发回,在描述中说

“该服务已关闭。有关详细信息,请参阅弃用服务博客文章http://developer.yahoo.com/blogs/ydn/posts/2010/08/api_updates_and_changes

这就是您无法访问 JSON 响应的预期属性的原因。

于 2012-07-19T12:23:35.433 回答