如何使用Rest API解析Twitter搜索结果 [英] How to parse twitter search result using rest API

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

问题描述

我试图解析使用rest API的结果.似乎get search/tweet API返回了Json对象,而不是Json数组.我的问题是:如果返回的整个对象是对象而不是数组,如何分隔返回的每个结果?

I tried to parse the result of using rest API. It seemed that the get search/tweet API returned Json object instead of Json array. My question is: how can I separate each result returned if the whole thing returned is an object instead of an array?

这是我的代码:

公共类搜索{

private static final String PROTECTED_RESOURCE_URL = "https://api.twitter.com/1.1/search/tweets.json";

public static void main(String[] args) {
    // If you choose to use a callback, "oauth_verifier" will be the return
    // value by Twitter (request param)
    String s="#NHL";
    System.out.println(getTweets(s));
}

public static String getTweetsString(String param) {
    OAuthService service = new ServiceBuilder()
            .provider(TwitterApi.SSL.class).apiKey(AuthInfo.API_KEY)
            .apiSecret(AuthInfo.API_SECRET).build();

    Token accessToken = new Token(AuthInfo.ACCESS_TOKEN,
            AuthInfo.ACCESS_TOKEN_SECRET);

    OAuthRequest request = new OAuthRequest(Verb.GET,
            PROTECTED_RESOURCE_URL);
    request.addQuerystringParameter("q",param);
    request.addQuerystringParameter("count", "2");

    service.signRequest(accessToken, request);
    Response response = request.send();

    System.out.println();
    return response.getBody();
}

public static ArrayList<Tweet> getTweets(String param){
    ArrayList<Tweet> result = new ArrayList<Tweet>();
    String tweets = getTweetsString(param);

    try {
        InputStream is = new ByteArrayInputStream(tweets.getBytes("UTF-8"));
        JsonReader reader = Json.createReader(is);
        JsonArray lists = reader.readArray();
        for(JsonObject rs: lists.getValuesAs(JsonObject.class)){
            System.out.println(rs);
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        return result;
}

}

显示以下错误: 无法读取JSON数组,找到JSON对象

The following error showed: Cannot read JSON array, found JSON object

感谢您的帮助!!!!

Thanks for you help!!!!

推荐答案

此链接应该可以帮助您进行Java的JSON解析(

This Link should get you going in your JSON parsing in Java(http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/)

但是由于您正在尝试解析Twitter api返回的JSON对象数组,因此请看下面的示例... http://www.bridgefarmconsulting.com/blog/twitter-authentication/ 在博客文章的结尾,您将看到作者编写了一个用于解析JSON对象的类... 因此,获取您的响应字符串,根据要访问的键创建解析器类并对其进行解析...

But since you are trying to parse the Array of JSON objects that Twitter api's return, have a look at this example... http://www.bridgefarmconsulting.com/blog/twitter-authentication/ In the end of the blog post you'll see that the writer has written a class which is being used for parsing the JSON object... So get your response string, create your parser class based on the keys which you would like to access and the parse it...

希望你有主意...

这篇关于如何使用Rest API解析Twitter搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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