سؤال

I have problem in parsing json from twitter search feed . for example the search url is:

https://search.twitter.com/search.json?q=android

Here is a link to the search

I want to get "result" array in json data. My code for fetch json and parse :

StringBuilder tweetFeedBuilder = new StringBuilder();
HttpClient tweetClient = new DefaultHttpClient();

//pass search URL string to fetch
HttpGet tweetGet = new HttpGet(searchURL);

//execute request
HttpResponse tweetResponse = tweetClient.execute(tweetGet);
//check status, only proceed if ok
StatusLine searchStatus = tweetResponse.getStatusLine();
if (searchStatus.getStatusCode() == 200) {
    //get the response
    HttpEntity tweetEntity = tweetResponse.getEntity();
    InputStream tweetContent = tweetEntity.getContent();
    //process the results
    InputStreamReader tweetInput = new InputStreamReader(tweetContent);
    BufferedReader tweetReader = new BufferedReader(tweetInput);

    while ((lineIn = tweetReader.readLine()) != null) 
    {
        tweetFeedBuilder.append(lineIn);
    }

    try{
        // A Simple JSONObject Creation
        JSONObject json=new JSONObject(tweetFeedBuilder);
        Log.i("Tweets","<jsonobject>\n"+json.toString()+"\n</jsonobject>");

        String str1 = "result";
        JSONArray jarray = json.getJSONArray(str1);

        for(int i = 0; i < jarray.length(); i++){

            JSONObject c = jarray.getJSONObject(i);
            String id = c.getString(TWEET_ID);
            String text = c.getString(TWEET_TEXT);
        }
    }
    catch(JSONException jexp){
        jexp.printStackTrace();
    }

After creating JSON object , JSONArray giving error in creating and goes in the catch block . Actually I want to fetch "result" array from JSON data.

But got error while creating. I just want to fetch user_id and text from the JSON data. I working in android platform and eclipse sdk.

هل كانت مفيدة؟

المحلول

As already mentioned in the comment, you are using the wrong key. It should be

String str1 = "results"; // you are using result

نصائح أخرى

(Just elaborating my comment...)

If you look closely in the JSON response, the key of the array of results is not result, but results. Therefore, you should get the JSONArray by doing this:

JSONArray jarray = json.getJSONArray("results");

Also, I noticed you wanted to fetch the "user_id" and "text" from each array item. Be sure to use the correct key for the user: from_user_id.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top