如何从 Twitter 搜索响应中解析 JSON [英] How do I parse JSON from a twitter search response

查看:25
本文介绍了如何从 Twitter 搜索响应中解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://search.twitter.com/search.json?page=10&q=&geocode=37,-122,1mi&callback=myCallback

这是我用于 twitter 搜索的网址.

This my url for a twitter search.

如何获取响应对象?我知道一般如何解析 JSON 对象,是一样的方式吗?

How do I get the response object? I know how to parse a JSON object in general, is it the same way?

谢谢!

推荐答案

尝试使用 BufferedReader 和 StringBuilder(这是我目前在我的应用程序中使用的).然后我使用 String 创建一个 JSONObject.以下是您可以使用的一些模板代码:

Try using a BufferedReader and StringBuilder (that's what I'm currently using for my application). I then use the String to create a JSONObject. Here's some template code you may use:

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(//YOUR URL STRING HERE);
    HttpResponse response;
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream in = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();

    String input = null;
    try {
        while ((input = reader.readLine()) != null) {
                    sb.append(input + "\n");
        }
    } catch (IOException e) {
            e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    String enter = sb.toString();
        JSONObject obj = new JSONObject(enter);

                //DO WHATEVER YOU WANT WITH THE JSONObject here

        in.close();
} catch(MalformedURLException e){
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e){
    e.printStackTrace();
}

这篇关于如何从 Twitter 搜索响应中解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆