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

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

问题描述

我尝试解析使用 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

谢谢你的帮助!!!!

推荐答案

此链接应该可以帮助您在 Java 中进行 JSON 解析(http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/)

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...

希望你有这个想法...

Hope you got the idea...

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

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