0

下面是我从 twitter 返回的 json

{
"created_at": "Sat, 11 Feb 2012 06:38:28 +0000",
"entities": {
  "hashtags": [
    {
      "text": "Shubhdin",
      "indices": [
        9,
        18
      ]
    }
  ],
  "urls": [],
  "user_mentions": [
    {
      "screen_name": "SAMdLaw",
      "name": "Sabyasachi Mohapatra",
      "id": 104420398,
      "id_str": "104420398",
      "indices": [
        0,
        8
      ]
    }
  ]
},
"from_user": "nilayshah80",
"from_user_id": 213599118,
"from_user_id_str": "213599118",
"from_user_name": "Nilay Shah",
"geo": {
  "coordinates": [
    18.6003,
    73.825
  ],
  "type": "Point"
},
"id": 168222351106899968,
"id_str": "168222351106899968",
"iso_language_code": "in",
"metadata": {
  "result_type": "recent"
},
"profile_image_url": "http://a2.twimg.com/profile_images/1528184590/IMG_0465_normal.JPG",
"profile_image_url_https": "https://si0.twimg.com/profile_images/1528184590/IMG_0465_normal.JPG",
"source": "<a href="http://twabbit.wordpress.com/" rel="nofollow">twabbit</a>",
"text": "@SAMdLaw #Shubhdin mitra",
"to_user": "SAMdLaw",
"to_user_id": 104420398,
"to_user_id_str": "104420398",
"to_user_name": "Sabyasachi Mohapatra",
"in_reply_to_status_id": 168219865197461505,
"in_reply_to_status_id_str": "168219865197461505"
},
{
"created_at": "Sun, 12 Feb 2012 01:54:07 +0000",
"entities": {
  "hashtags": [
    {
      "text": "IWIllAlwaysLoveYou",
      "indices": [
        88,
        107
      ]
    }
  ],
  "urls": [],
  "user_mentions": [],
  "media": [
    {
      "id": 168513175187238912,
      "id_str": "168513175187238912",
      "indices": [
        108,
        128
      ],
      "media_url": "http://p.twimg.com/Alat1wsCMAAh-wE.jpg",
      "media_url_https": "https://p.twimg.com/Alat1wsCMAAh-wE.jpg",
      "url": "http://shortener.twitter.com/dRc4dXH3",
      "display_url": "pic.twitter.com/dRc4dXH3",
      "expanded_url": "http://twitter.com/RIPWhitneyH/status/168513175183044608/photo/1",
      "type": "photo",
      "sizes": {
        "orig": {
          "w": 395,
          "h": 594,
          "resize": "fit"
        },
        "large": {
          "w": 395,
          "h": 594,
          "resize": "fit"
        },
        "thumb": {
          "w": 150,
          "h": 150,
          "resize": "crop"
        },
        "small": {
          "w": 340,
          "h": 511,
          "resize": "fit"
        },
        "medium": {
          "w": 395,
          "h": 594,
          "resize": "fit"
        }
      }
    }
  ]
},
"from_user": "RIPWhitneyH",
"from_user_id": 19319043,
"from_user_id_str": "19319043",
"from_user_name": "RIP Whitney Houston",
"geo": null,
"id": 168513175183044608,
"id_str": "168513175183044608",
"iso_language_code": "en",
"metadata": {
  "recent_retweets": 8,
  "result_type": "popular"
},
"profile_image_url": "http://a2.twimg.com/profile_images/1820957590/images__13__normal.jpg",
"profile_image_url_https": "https://si0.twimg.com/profile_images/1820957590/images__13__normal.jpg",
"source": "<a href="http://twitter.com/">web</a>",
"text": "R-T if you think that the Grammy's should organize an \"R.I.P. Whitney Houston\" tribute. #IWIllAlwaysLoveYou http://shortener.twitter.com/dRc4dXH3",
"to_user": null,
"to_user_id": null,
"to_user_id_str": null,
"to_user_name": null
},

如果您注意到上述 2 中不可用的实体下的媒体,并且当我尝试调用下面的代码片段时,会出现空引用错误

 MediaUrl = (from user in tweet["entities"]["media"]
 select new mediaUrl
 {
      shortUrl = (string)user["url"],
      longUrl = (string)user["expanded_url"],
      url = (string)user["media_url"],
      start = user["indices"][0].ToString(),
      end = user["indices"][1].ToString(),
      mediaType = (string)user["type"],
  }).ToList()

相同的代码适用于实体/URL、标签和提及,但不适用于媒体。

也试过这个 ->获取 JSON 对象节点,但仍然得到空引用异常。

4

2 回答 2

0

在第一条推文中,entities对象没有media属性,因此在评估第一条推文时,您的代码将等效于:

 MediaUrl = (from user in (IEnumerable<JToken>)null
 select new mediaUrl
 {
      shortUrl = (string)user["url"],
      longUrl = (string)user["expanded_url"],
      url = (string)user["media_url"],
      start = user["indices"][0].ToString(),
      end = user["indices"][1].ToString(),
      mediaType = (string)user["type"],
  }).ToList()

这将抛出ArgumentNullException,因为该代码确实查询空引用集合。

于 2012-02-13T02:30:50.203 回答
0

终于开工了。不是合适的解决方案,但对我有用。

我为解析媒体创建了单独的方法。将实体作为字符串传递,在该方法中我检查的是 EntityString.Contains Media 与否。如果是,则解析媒体 json,否则返回 null。见下文片段。

        if (Entities != string.Empty)
        {
            if (Entities.Contains("\"media\":"))
            {
                JObject searchResult = JObject.Parse(Entities);
                returnMedia = (from user in searchResult["media"]
                               select new mediaUrl
                               {
                                   shortUrl = (string)user["url"],
                                   longUrl = (string)user["expanded_url"],
                                   url = (string)user["media_url"],
                                   start = user["indices"][0].ToString(),
                                   end = user["indices"][1].ToString(),
                                   mediaType = (string)user["type"],
                               }).ToList();
            }
        }

这对我有用。如果您有更好的解决方案,请告诉我。

于 2012-02-16T17:49:31.203 回答