【问题标题】:Error parsing twitter json response in android在android中解析twitter json响应时出错
【发布时间】:2012-05-24 02:25:55
【问题描述】:

我尝试使用 Twitter Get users/lookup 来查找用户信息。但是我在解析响应 json 文件时遇到了一些错误。请求 URI 为:“https://api.twitter.com/1/users/lookup.json?screen_name=nba”。我的代码是:

public String getInternetData() throws Exception{
    String data = null;
    try {
        URI website = new URI("https://api.twitter.com/1/users/lookup.json?screen_name=nba");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String str = EntityUtils.toString(entity);
        try {
            JSONObject jouser = new JSONObject(str);
            data = jouser.getString("followers_count");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection: " +e.toString());
    }
    return data;
}

响应的 JSON 内容为

[
   {
      "notifications":false,
      "id":19923144,
      "profile_link_color":"177BC7",
      "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1787324427\/National-Basketball-Association_normal.jpg",
      "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/559347559\/12twitter_playoffs_0523.jpg",
      "id_str":"19923144",
      "following":false,
      "profile_use_background_image":true,
      "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1787324427\/National-Basketball-Association_normal.jpg",
      "utc_offset":-18000,
      "friends_count":988,
      "profile_text_color":"333333",
      "time_zone":"Eastern Time (US & Canada)",
      "default_profile":false,
      "followers_count":5095414,
      "name":"NBA",
      "profile_banner_url":"https:\/\/si0.twimg.com\/brand_banners\/NBA\/1335482314\/live",
      "url":"http:\/\/www.nba.com",
      "profile_sidebar_border_color":"eeeeee",
      "created_at":"Mon Feb 02 19:04:42 +0000 2009",
      "protected":false,
      "listed_count":28499,
      "profile_background_tile":false,
      "contributors_enabled":true,
      "profile_sidebar_fill_color":"ffffff",
      "geo_enabled":false,
      "description":"News and notes directly from the NBA.",
      "location":"New York, NY",
      "is_translator":false,
      "show_all_inline_media":true,
      "statuses_count":28818,
      "follow_request_sent":false,
      "lang":"en",
      "profile_background_color":"000000",
      "default_profile_image":false,
      "verified":true,
      "favourites_count":15,
      "screen_name":"NBA",
      "profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/559347559\/12twitter_playoffs_0523.jpg"
   }
]

然后我得到错误: *Error parsing data: org.json.JSONException: Value [{"location"...... 有人有想法吗?

【问题讨论】:

    标签: android json twitter lookup


    【解决方案1】:

    如果响应以[ 开头,则意味着它实际上是一个 JSON 数组,而不是一个对象。然后,您将不得不执行以下操作:

    JSONArray array = new JSONArray(str);
    JSONObject jsonObject = array.getJSONObject(0);
    String followersCount = jsonObject.getString("followers_count");
    

    【讨论】:

    • 它确实以 [.但我找不到这个数组的名称。就像在这个文件中一样,我应该在这个表达式中用什么参数替换“pepe”: JSONArray array = new JSONArray(pepe) ?
    • 佩佩?什么是佩佩?你在说什么?
    • 你是对的。这是一个 JSONArray,而不是 JSONObject。 JSONArray 数组 = 新 JSONArray(str);解决问题。谢谢身体!
    • 另一个问题:如何判断响应JSON文件中的数据类型?像“followers_count”:5414。如何确定它是 int 还是 long 类型?
    • 除了尝试 getInt 或 getLong 之类的方法并捕获异常之外,没有其他办法(除了事先知道 JSON 数据)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多