【问题标题】:How do I parse JSON from a twitter search response如何从 twitter 搜索响应中解析 JSON
【发布时间】:2011-08-10 20:55:52
【问题描述】:

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

这是我的推特搜索网址。

如何获取响应对象?我一般都知道怎么解析一个JSON对象,是不是一样的?

谢谢!

【问题讨论】:

    标签: android twitter


    【解决方案1】:

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

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

    【讨论】:

    • 注意这里使用的是org.json下的Simple JSON库
    • 谢谢 Vinay,我不知道我应该这样做。
    猜你喜欢
    • 2013-01-19
    • 2013-04-12
    • 2015-01-14
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多