Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
媒体不使用Json.net for Windows phone Mango silverlight的Twitter搜索api实体_Silverlight_Json.net_Windows Phone 7_Twitter Search - Fatal编程技术网

媒体不使用Json.net for Windows phone Mango silverlight的Twitter搜索api实体

媒体不使用Json.net for Windows phone Mango silverlight的Twitter搜索api实体,silverlight,json.net,windows-phone-7,twitter-search,Silverlight,Json.net,Windows Phone 7,Twitter Search,下面是我从twitter返回的json { "created_at": "Sat, 11 Feb 2012 06:38:28 +0000", "entities": { "hashtags": [ { "text": "Shubhdin", "indices": [ 9, 18 ] } ], "urls": [], "user_mentions": [ { "screen_na

下面是我从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、哈希标记和提及,但不适用于媒体


也尝试了此->但仍然得到空引用异常。

在第一条tweet中,
实体
对象没有
媒体
属性,因此在评估第一条tweet时,您的代码将等效于:

 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()
MediaUrl=(来自(IEnumerable)中的用户)null
选择新的mediaUrl
{
shortUrl=(字符串)用户[“url”],
longUrl=(字符串)用户[“扩展的url”],
url=(字符串)用户[“媒体url”],
开始=用户[“索引”][0]。ToString(),
end=user[“index”][1]。ToString(),
mediaType=(字符串)用户[“类型”],
})托利斯先生()

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

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

我为解析媒体创建了单独的方法。以字符串形式传递实体,并且在我选中的方法中为EntityString。是否包含媒体。如果是,则parse media json-else返回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();
            }
        }

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

如果您将处理程序附加到JsonSerializerSettings中的错误事件,您会得到更多有用的信息吗?不尝试使用处理程序。我会在这里检查并报告。谢谢,但我还是不明白。如果我尝试使用null,它将返回我ArgumentNullException,并且它也不会与第二条tweet一起工作。我的意思是第二条tweet如何传递媒体。问题在于第一条tweet,而不是第二条tweet,这会导致代码抛出空引用异常。这是因为在第一条tweet中,实体
对象
没有
媒体
属性,因此
tweet[“实体”][“媒体”]
将返回
null
非空集合。尝试通过从json中删除第一条tweet来测试它。您的代码不会引发异常。是的,如果我删除第一个异常,那么它就可以工作。但我的问题是来自twitter,我只收到这类推文。