2012-05-17 73 views
0

我有下面的方法來解析來自Twitter的搜索數據,奇怪的是這種方法能正常工作在我的形式之一,但在我的主要形式,它返回異常:Twitter的JSON - 奇怪的錯誤

異常:「System.Collections.Generic.Dictonary」不 包含「文本」

兩種形式幾乎是相同的,我不明白爲什麼我不能得到的代碼工作的定義..任何人有任何想法?

下面是方法:我收到了JSON

public static HashSet<string> searchTwitterJson(string searchTerm) 
    { 
     HashSet<string> resultsFound = new HashSet<string>(); 


     if (searchTerm != "") 
     { 
      string v = searchTerm.Replace("\"", "%22"); 
      string keyword = v.Trim(); 
      string keywordet = HttpUtility.UrlEncode(keyword); 


      try 
      { 
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://twitter.com/phoenix_search.phoenix?q=" + keywordet + "&headers[X-Twitter-Polling]=true&headers[X-PHX]=true&since_id=203194965877194752&include_entities=1&include_available_features=1&contributor_details=true&mode=relevance&query_source=unknown"); 
       var response = request.GetResponse(); 
       var reader = new StreamReader(response.GetResponseStream()); 
       var responseString = reader.ReadToEnd(); 

       string limit = response.Headers["X-RateLimit-Remaining"]; 


       var serializer = new JavaScriptSerializer(); 
       serializer.RegisterConverters((new[] { new DynamicJsonConverter() })); 
       dynamic obj = serializer.Deserialize(responseString, typeof(object)) as dynamic; 

       foreach (var objects in obj.statuses) 
       { 
        if ((objects.text != null) && (objects.user.screen_name != null) && (objects.id_str != null)) 
        { 
         Match m = Regex.Match(objects.text, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\S*)?"); 


         if (!m.Success) 
         { 
          string loggaD = objects.user.screen_name.ToString() + "/" + objects.id_str.ToString(); 
          resultsFound.Add(loggaD); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
     else 
     { 
      MessageBox.Show("No searchterm"); 
     } 

     return resultsFound; 
    } 

部分:

{ 
    "error": null, 
    "statuses": [{ 
     "in_reply_to_status_id_str": null, 
     "id_str": "203239104421445632", 
     "truncated": false, 
     "possibly_sensitive": false, 
     "created_at": "Thu May 17 21:42:33 +0000 2012", 
     "in_reply_to_user_id_str": null, 
     "contributors": null, 
     "favorited": false, 
     "geo": null, 
     "user": { 
      "screen_name": "YouthNorrort" 
     }, 
     "in_reply_to_screen_name": null, 
     "coordinates": null, 
     "retweet_count": 0, 
     "source": "\u003Ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003EFacebook\u003C\/a\u003E", 
     "place": null, 
     "in_reply_to_status_id": null, 
     "id": 203239104421445632, 
     "retweeted": false, 
     "in_reply_to_user_id": null, 
     "text": "Hejsan igen!\nvi \u00e4r ledsna att meddela er, men inf\u00f6r imorgon s\u00e5 finns det inga bilar, allts\u00e5 inte heller plats f\u00f6r... http:\/\/t.co\/pMKoOz7o", 
     "result_category": "recent" 
    } 

回答

1

錯誤說,反應不會在節點包含在「狀態」的項目。[一世]。檢查正在返回的JSON,如果沒有,則不能嘗試訪問它。

{ 
    "statuses": 
    [ 
     { 
      "text": ..., 
      "user: { "screen_name": ... } 
     } 
    ] 
} 

代碼:

if ((objects.text != null) && (objects.user.screen_name != null) && (objects.id_str != null)) 

更新:根據您的JSON例子,它工作正常(見下文)。狀態數組中的所有元素是否包含「文本」成員?

public static void Test() 
    { 
     string json = @"{ 
""error"": null, 
""statuses"": [{ 
    ""in_reply_to_status_id_str"": null, 
    ""id_str"": ""203239104421445632"", 
    ""truncated"": false, 
    ""possibly_sensitive"": false, 
    ""created_at"": ""Thu May 17 21:42:33 +0000 2012"", 
    ""in_reply_to_user_id_str"": null, 
    ""contributors"": null, 
    ""favorited"": false, 
    ""geo"": null, 
    ""user"": { 
     ""screen_name"": ""YouthNorrort"" 
    }, 
    ""in_reply_to_screen_name"": null, 
    ""coordinates"": null, 
    ""retweet_count"": 0, 
    ""source"": ""\u003Ca href=\""http:\/\/www.facebook.com\/twitter\"" rel=\""nofollow\""\u003EFacebook\u003C\/a\u003E"", 
    ""place"": null, 
    ""in_reply_to_status_id"": null, 
    ""id"": 203239104421445632, 
    ""retweeted"": false, 
    ""in_reply_to_user_id"": null, 
    ""text"": ""Hejsan igen!\nvi \u00e4r ledsna att meddela er, men inf\u00f6r imorgon s\u00e5 finns det inga bilar, allts\u00e5 inte heller plats f\u00f6r... http:\/\/t.co\/pMKoOz7o"", 
    ""result_category"": ""recent"" 
}]}"; 
     JavaScriptSerializer jss = new JavaScriptSerializer(); 
     jss.RegisterConverters(new[] { new DynamicJsonConverter() }); 
     dynamic obj = jss.Deserialize(json, typeof(object)) as dynamic; 

     foreach (var objects in obj.statuses) 
     { 
      Console.WriteLine(objects.GetType()); 
      if (objects.ContainsKey("text")) 
      { 
       if ((objects.text != null) && (objects.user.screen_name != null) && (objects.id_str != null)) 
       { 
        Match m = Regex.Match(objects.text, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\S*)?"); 

        if (!m.Success) 
        { 
         string loggaD = objects.user.screen_name.ToString() + "/" + objects.id_str.ToString(); 
         Console.WriteLine(loggaD); 
        } 
       } 
      } 
     } 

而對於TryInvokeMember代碼:

  public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) 
      { 
       if (binder.Name == "ContainsKey") 
       { 
        result = _dictionary.ContainsKey(args[0] as string); 
        return true; 
       } 
       else 
       { 
        return base.TryInvokeMember(binder, args, out result); 
       } 
      } 
+0

超奇怪的是,菲德勒這顯示在開頭:{ 「錯誤」:空, 「狀態」:[{ 「in_reply_to_status_id_str」: ... – user1213488

+0

和我看到文字和其他人 – user1213488

+0

你可以發佈你收到的JSON嗎? – carlosfigueira