简体   繁体   English

在C#中反序列化动态对象

[英]Deserialize dynamic object in c#

I am trying to fetch Facebook profile data through Graph API. 我正在尝试通过Graph API获取Facebook个人资料数据。 I am using Facebook SDK in MVC application. 我在MVC应用程序中使用Facebook SDK。 Using the following code returns dynamic result. 使用以下代码返回动态结果。

dynamic myInfo = await fb.GetTaskAsync("v2.8/me?fields=first_name,last_name,link,locale,email,name,birthday,gender,location,age_range");
      var facebookProfile = new FacebookProfileViewModel()
                {
                    FirstName = myInfo.first_name,
                    LastName = myInfo.last_name,
                    LinkUrl = myInfo.link,
                    Locale = myInfo.locale };

I am getting the data from facebook like this - 我正在像这样从facebook获取数据-

[{"Key":"first_name","Value":"John"},{"Key":"last_name","Value":"Doe"},{"Key":"link","Value":"https://www.facebook.com/app_scoped_user_id/4327693/"}]

I am getting the following error - 我收到以下错误-

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'Facebook.JsonObject' to 'string'

Install newtonsoft: 安装newtonsoft:

Install-Package Newtonsoft.Json 安装包Newtonsoft.Json

Convert from json: 从json转换:

string json = 
"[
    {
        "Key": "first_name",
        "Value": "John"
    },
    {
        "Key": "last_name",
        "Value": "Doe"
    },
    {
        "Key": "link",
        "Value": "https://www.facebook.com/app_scoped_user_id/4327693/"
    }
]";
FbProfile Profile = JsonConvert.DeserializeObject<FbProfile>(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM