0

这是我的 JSON 的一部分:

  [
  UserJSONImpl{
    id=1489761876,
    name='CharlesPerin',
    screenName='charles_perin',
    location='Paris,
    France',
    description='PhdStudentatINRIA-Univ.Paris-Sud-CNRS-LIMSI#infovis#dataviz#hci',
    isContributorsEnabled=false,
    profileImageUrl='http: //a0.twimg.com/profile_images/3766400220/bbced44afe69e60eb30e00f593a2f3b5_normal.jpeg',
    profileImageUrlHttps='https: //si0.twimg.com/profile_images/3766400220/bbced44afe69e60eb30e00f593a2f3b5_normal.jpeg',
    url='http: //t.co/eYSy04EzEk',
    isProtected=false,
    },
    UserJSONImpl{
        id=19671465,
        name='KevinQuealy',
        screenName='KevinQ',
        location='NewYork,
        NY',
        description='AgraphicseditorattheNewYorkTimes.AdjunctatNYU#SHERP.ReturnedPeaceCorpsvolunteer.Bald,
        Minnesotan,
        talkstoomuch.',
        isContributorsEnabled=false,
        profileImageUrl='http: //a0.twimg.com/profile_images/2213326305/image_normal.jpg',
        profileImageUrlHttps='https: //si0.twimg.com/profile_images/2213326305/image_normal.jpg',
        url='http: //t.co/vb0j99kE3N',
        isProtected=false,
        ...(cont)

这是直接从对 twitter4j 的 lookupUsers 的调用中返回的:

long[] hundredIDs = new long[100];
  org.json.JSONArray users = new org.json.JSONArray();
  for(int a = 0; a < (int)((double)friendArray.length()/100 +1); a++)
  {   

      for(int j = 100*a; j < 100*(a+1); j++)
      {
        hundredIDs[j-100*a] = Long.parseLong(friendArray.getString(j)); 
      }
      users = new org.json.JSONArray(twitter.lookupUsers(hundredIDs)); //lookup users in batches of 100


    for(int k = 0; k < users.length(); k++)
    {
      org.json.JSONObject user = users.getJSONObject(k);
      if(Long.parseLong(user.getString("followers_count")) >= 500)
      {
        String id = user.getString("id"); //get id for each JSONObject
        friendArrayFiltered.add(id); //store ids in another array
      }
    }

出于某种原因,我的代码返回的 JSON 没有围绕属性的标准引号(“id”= ....,而不是 id =...)。这似乎不是 Twitter API 本身的问题,因为它们的示例格式正确:https ://dev.twitter.com/docs/api/1/get/users/lookup 。有谁知道问题是什么?

此外,不确定这是否是结果,但是当我尝试访问 JSONArray 的单个元素(如 JSONArray[0])时,返回错误,指出 JSONArray[0] 不是 JSONObject。这与上述问题有关吗?

4

1 回答 1

0

它不是 JSON,它实际上是由为调用返回的UserJSONImpl#toString()每个对象提供文本表示的方法生成的。UserlookupUsers

至于您的第二个问题,您不能在 Java[]中的类型上使用运算符,Object所以如果没有更多信息,我有点不清楚您的意思。

在旁边

我不确定你为什么将 twitter4j 对象包装在JSONArrayJSONObject对象中——当然你可能有充分的理由这样做,这在问题中并不明显——但你可以简单地直接在返回的对象上使用这些方法来获取你的信息需要,例如:

final List<User> users = twitter.lookupUsers(hundredIDs);
for (User user : users) {
    final int followersCount = user.getFollowersCount();
    if (followersCount > 500) { 
        ... etc...

查看项目的UserJavaDocs和更广泛的文档

于 2013-08-22T15:27:03.557 回答