2016-08-03 59 views
0

有點解釋。我正在使用Facebook API與我的C#應用​​程序集成。由於Facebook已經更新了API。我有問題保持由Facebook返回的JSON結果在我的代碼中使用。以前由Facebook在JSON對象中返回的結果。我知道如何使用JSON對象。如何從JSON字符串中獲取數據

問題現在是JSON返回爲字符串。我能做些什麼來捕獲JSON字符串中的數據?

這是一個被Facebook

{"id":"t_mid.1468318160994:4fbaede7284fc37853","messages":{"data":[{"created_time":"2016-07-12T10:09:21+0000","id":"m_mid.1468318160994:4fbaede7284fc37853"}],"paging":{"previous":"https://graph.facebook.com/v2.6/t_mid.1468318160994:4fbaede7284fc37853/messages?access_token=[ACCESS-TOKEN]&limit=25&since=1468318161&__paging_token=[ACCESS-TOKEN]&__previous=1","next":"https://graph.facebook.com/v2.6/t_mid.1468318160994:4fbaede7284fc37853/messages?access_token=[ACCESS-TOKEN]&limit=25&until=1468318161&__paging_token=[ACCESS-TOKEN]"}}} 
    base {System.Dynamic.DynamicObject}: {"id":"t_mid.1468318160994:4fbaede7284fc37853","messages":{"data":[{"created_time":"2016-07-12T10:09:21+0000","id":"m_mid.1468318160994:4fbaede7284fc37853"}],"paging":{"previous":"https://graph.facebook.com/v2.6/t_mid.1468318160994:4fbaede7284fc37853/messages?access_token=[ACCESS-TOKEN]&__previous=1","next":"https://graph.facebook.com/v2.6/t_mid.1468318160994:4fbaede7284fc37853/messages?access_token=[ACCESS-TOKEN]&limit=25&until=1468318161&__paging_token=[ACCESS-TOKEN]"}}} 
    Count: 2 
    IsReadOnly: false 
    Keys: Count = 2 
    Values: Count = 2 

給出的JSON這是我的代碼從字符串

public class GetMesaggeIDUsingConversationID_API_V2_6 
{ 
    public messages id { get; set; } 
    public class messages 
    { 
     public Data[] data { get; set; } 
     public class Data 
     { 
      public string id { get; set; } 
     } 
    } 
} 

List<GetMesaggeIDUsingConversationID_API_V2_6> msgApiforGetMsgID = JsonConvert.DeserializeObject<List<GetMesaggeIDUsingConversationID_API_V2_6>>("JSON STRING HERE"); 

獲取數據但它成爲錯誤。

的錯誤是:

"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<Orlig_Server_Services.Components.Refer.GetMesaggeIDUsingConversationID_API_V2_6>>(string)' has some invalid arguments\r\n at CallSite.Target(Closure , CallSite , Type , Object)\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)\r\n at Orlig_Server_Services.Components.FacebookIntegration.getFanMessage() in D:\\Projects\\Orlig_FB_Service V3.2 API V2.6\\Components\\FacebookIntegration.cs:line 2874" 

請給我一些建議。提前致謝!

+0

你的facebook api json似乎無效。我檢查了它,格式結果似乎不正確。它甚至不是一個數組,所以你可能需要做一些解耦的反應,然後用Json.Net 反序列化它看看這裏:http://clip2net.com/s/3APtRnE – Jeff

回答

0

您的字符串是JSON格式。你想解析它們,所以你需要使用JSON.NET

string source = "JSON..." 
dynamic data = JObject.Parse(source); 

Console.WriteLine(data.id); 
Console.WriteLine(data.messages); 
// ...