0

I am trying to pass a Json response string as an argument to a jsonObject as illustrated by Yahoo http://developer.yahoo.com/java/howto-parseRestJava.html in the following given code, this is in order to get yahoo search results:

/**
 * 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("--");
        }
    }
}

After adding the required code for authentication, and include the required jars, I get the following error:

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)

This is the beginning of the Json response string:

{"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":

looking at the response, and the objects names, it seems that Yahoo did some changes in their response, and I changed the following two lines of their code to:

// 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");

I still get the error:

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)

I don't know where is the problem. This is the first time I do json parsing. I might have misunderstanding in a point or another. Please, clarify.

Rob
  • 4,927
  • 12
  • 49
  • 54
Jury A
  • 19,192
  • 24
  • 69
  • 93

1 Answers1

0

The above mentioned yahoo URL is sending back error as json response which says in description

"The service has been shut down. For further details, please see the Deprecated Services blog post http://developer.yahoo.com/blogs/ydn/posts/2010/08/api_updates_and_changes"

That is the reason why you are not able to access expected attributes of JSON Response.