0

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.

4

2 回答 2

1

正如评论中已经提到的,您使用了错误的密钥。它应该是

String str1 = "results"; // you are using result
于 2013-04-14T16:09:45.553 回答
0

(只是详细说明我的评论......)

如果您仔细查看 JSON 响应,结果数组的键不是result,而是results。因此,您应该通过以下方式获取 JSONArray:

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

另外,我注意到您想从每个数组项中获取“user_id”和“text”。确保为用户使用正确的密钥:from_user_id

于 2013-04-14T17:43:33.543 回答