反序列化动态对象 [英] deserialize dynamic object

查看:74
本文介绍了反序列化动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试找出如何通过调用Facebook api反序列化我的响应时遇到了很多麻烦.我的C#代码如下:

I am having a lot of trouble trying to figure out how to deserialize my response from making calls to facebook api. my c# code looks like this:

FacebookClient client = new FacebookClient();
string path = "SELECT message, type, attachment FROM stream WHERE source_id = " + fbid.ToString();
dynamic result = client.Get("fql",     new { q = path });

结果是这样-它以json的形式返回

this is what result looks like - it comes back as json

我需要反序列化此Dynamic对象,它是将字符串json转换为c#对象.不知道可以对动态对象执行foreach循环.我还尝试只创建一个对象,然后反序列化到我创建的对象.问题是,正如您所看到的,有很多属性大多数时候都没有返回,然后有时它们会返回.因此,Facebook返回的json始终具有不同数量的属性.我尝试执行一个for循环,然后通过诸如response.data.message [1]之类的方法将对象从json中取出,但这是行不通的.有什么想法可以将这个动态对象反序列化为我可以使用的东西吗?

I need to deserialize this Dynamic object , which is a string json into a c# object. don't know that it is possible to do a foreach loop on a dynamic object. I also tried to just create an object and the deserialize to the object I created. The problem is that as you can see there are a lot of properties that are not returned most of the time , then some times they are returned. So the json that Facebook is returning always has a different amount of properties. I tried doign a for loop then getting the objects out of json by something like response.data.message[1] , but that does not work. Any ideas how to get this dynamic object deserialized into somethign that I can use?

{"data":
 [
    {
        "message": "",
        "type": null,
        "attachment": {
            "description": ""
        }
    },
    {
        "message": "",
        "type": 80,
        "attachment": {
            "media": [
                {
                    "href": "https://www.facebook.com/pages/Team-Vintage/119337701414139",
                    "alt": "",
                    "type": "link",
                    "src": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/71056_119337701414139_6207793_n.jpg"
                }
            ],
            "name": "Team Vintage",
            "href": "https://www.facebook.com/pages/Team-Vintage/119337701414139",
            "caption": "",
            "description": "Team Vintage is designed to handle all your event planning needs.  We cater to individuals as well as groups and businesses focused on sporting events, concerts, tailgates, golf outings, as well as transportation via limos and party/coach buses.",
            "properties": [
                {
                    "name": "Page",
                    "text": "245 like this"
                }
            ],
            "icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yf/r/0HZPW6-lhQu.png"
        }
    },
    {
        "message": "",
        "type": 237,
        "attachment": {
            "media": [
                {
                    "href": "",
                    "alt": "",
                    "type": "swf",
                    "src": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=p&cksum=7d97808202a372d6392257f52d91e5d2&src=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000027234998-gkd71f-t500x500.jpg%3Fcc07a88",
                    "swf": {
                        "source_url": "http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F50851560&color=3b5998&auto_play=true&show_artwork=false&origin=facebook",
                        "preview_img": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=p&cksum=7d97808202a372d6392257f52d91e5d2&src=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000027234998-gkd71f-t500x500.jpg%3Fcc07a88",
                        "width": 460,
                        "height": 98,
                        "expanded_width": 0,
                        "expanded_height": 0
                    }
                }
            ],
            "name": "Camp Bisco",
            "href": "http://soundcloud.com/amon-tobin/bed-time-stories-extended-live",
            "caption": "",
            "description": "Check out this extended track from Amon Tobin!",
            "properties": [],
            "icon": "https://www.facebook.com/images/icons/default_app_icon.gif"
        }
    },
    {
        "message": "",
        "type": null,
        "attachment": {
            "description": ""
        }
    },
    {
        "message": "",
        "type": 237,
        "attachment": {
            "media": [
                {
                    "href": "https://dev.fanfueledengage.com/share/auitbc",
                    "alt": "",
                    "type": "link",
                    "src": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=z&cksum=124f2c1fb6ea2fcaa8d98ebe0fd2246d&src=http%3A%2F%2Fimages.proboardshop.com%2Fbigagnes-elkpark20-long-right-sleepingbag-coalrust-13-thumb.jpg"
                }
            ],
            "name": "Proboardshop",
            "href": "https://dev.fanfueledengage.com/share/auitbc",
            "caption": "dev.fanfueledengage.com",
            "description": "I just bought  Big Agnes Elk Park 20 Long Right Sleeping Bag Coal/Rust  on FanFueled. I'm earning Camp Bisco points. Check it out. If you decide to make a purchase through my link, FanFueled will pay me a commission for it, cool huh?",
            "properties": [],
            "icon": "https://www.facebook.com/images/icons/default_app_icon.gif"
        }
    },
    {
        "message": "",
        "type": 8,
        "attachment": {
            "description": ""
        }
    },
    {
        "message": "",
        "type": 80,
        "attachment": {
            "media": [
                {
                    "href": "https://www.facebook.com/pages/Magnolia-Roller-Vixens/29048470621",
                    "alt": "",
                    "type": "link",
                    "src": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/276513_29048470621_1691556_n.jpg"
                }
            ],
            "name": "Magnolia Roller Vixens",
            "href": "https://www.facebook.com/pages/Magnolia-Roller-Vixens/29048470621",
            "caption": "",
            "description": "Magnolia Roller Vixens is a community-wise sports initiative for adult females. Our purpose is for fun, friendly sports competition among like-minded women.  Magnolia Roller Vixens promotes and fosters the sport of women's flat track derby by facilitating the development of athletic ability, sportsw...",
            "properties": [
                {
                    "name": "Page",
                    "text": "2,717 like this"
                }
            ],
            "icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yf/r/0HZPW6-lhQu.png"
        }
    }
 ]
}

推荐答案

使用示例json,以下代码对我有效,

Using your sample json, following code works for me,

dynamic response = JsonConvert.DeserializeObject(json);

Console.WriteLine("PropName:" + response.data[1].attachment.properties[0].name);

foreach (var data in response.data)
{
    if (data.attachment != null)
        Console.WriteLine((string)data.attachment.name);
}

PS:我使用了 Json.Net

string json = @"{""data"":
 [
    {
        ""message"": """",
        ""type"": null,
        ""attachment"": {
            ""description"": """"
        }
    },
    {
        ""message"": """",
        ""type"": 80,
        ""attachment"": {
            ""media"": [
                {
                    ""href"": ""https://www.facebook.com/pages/Team-Vintage/119337701414139"",
                    ""alt"": """",
                    ""type"": ""link"",
                    ""src"": ""https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/71056_119337701414139_6207793_n.jpg""
                }
            ],
            ""name"": ""Team Vintage"",
            ""href"": ""https://www.facebook.com/pages/Team-Vintage/119337701414139"",
            ""caption"": """",
            ""description"": ""Team Vintage is designed to handle all your event planning needs.  We cater to individuals as well as groups and businesses focused on sporting events, concerts, tailgates, golf outings, as well as transportation via limos and party/coach buses."",
            ""properties"": [
                {
                    ""name"": ""Page"",
                    ""text"": ""245 like this""
                }
            ],
            ""icon"": ""https://fbstatic-a.akamaihd.net/rsrc.php/v2/yf/r/0HZPW6-lhQu.png""
        }
    },
    {
        ""message"": """",
        ""type"": 237,
        ""attachment"": {
            ""media"": [
                {
                    ""href"": """",
                    ""alt"": """",
                    ""type"": ""swf"",
                    ""src"": ""https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=p&cksum=7d97808202a372d6392257f52d91e5d2&src=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000027234998-gkd71f-t500x500.jpg%3Fcc07a88"",
                    ""swf"": {
                        ""source_url"": ""http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F50851560&color=3b5998&auto_play=true&show_artwork=false&origin=facebook"",
                        ""preview_img"": ""https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=p&cksum=7d97808202a372d6392257f52d91e5d2&src=http%3A%2F%2Fi1.sndcdn.com%2Fartworks-000027234998-gkd71f-t500x500.jpg%3Fcc07a88"",
                        ""width"": 460,
                        ""height"": 98,
                        ""expanded_width"": 0,
                        ""expanded_height"": 0
                    }
                }
            ],
            ""name"": ""Camp Bisco"",
            ""href"": ""http://soundcloud.com/amon-tobin/bed-time-stories-extended-live"",
            ""caption"": """",
            ""description"": ""Check out this extended track from Amon Tobin!"",
            ""properties"": [],
            ""icon"": ""https://www.facebook.com/images/icons/default_app_icon.gif""
        }
    },
    {
        ""message"": """",
        ""type"": null,
        ""attachment"": {
            ""description"": """"
        }
    },
    {
        ""message"": """",
        ""type"": 237,
        ""attachment"": {
            ""media"": [
                {
                    ""href"": ""https://dev.fanfueledengage.com/share/auitbc"",
                    ""alt"": """",
                    ""type"": ""link"",
                    ""src"": ""https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=263101677125815&v=1&size=z&cksum=124f2c1fb6ea2fcaa8d98ebe0fd2246d&src=http%3A%2F%2Fimages.proboardshop.com%2Fbigagnes-elkpark20-long-right-sleepingbag-coalrust-13-thumb.jpg""
                }
            ],
            ""name"": ""Proboardshop"",
            ""href"": ""https://dev.fanfueledengage.com/share/auitbc"",
            ""caption"": ""dev.fanfueledengage.com"",
            ""description"": ""I just bought  Big Agnes Elk Park 20 Long Right Sleeping Bag Coal/Rust  on FanFueled. I'm earning Camp Bisco points. Check it out. If you decide to make a purchase through my link, FanFueled will pay me a commission for it, cool huh?"",
            ""properties"": [],
            ""icon"": ""https://www.facebook.com/images/icons/default_app_icon.gif""
        }
    },
    {
        ""message"": """",
        ""type"": 8,
        ""attachment"": {
            ""description"": """"
        }
    },
    {
        ""message"": """",
        ""type"": 80,
        ""attachment"": {
            ""media"": [
                {
                    ""href"": ""https://www.facebook.com/pages/Magnolia-Roller-Vixens/29048470621"",
                    ""alt"": """",
                    ""type"": ""link"",
                    ""src"": ""https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/276513_29048470621_1691556_n.jpg""
                }
            ],
            ""name"": ""Magnolia Roller Vixens"",
            ""href"": ""https://www.facebook.com/pages/Magnolia-Roller-Vixens/29048470621"",
            ""caption"": """",
            ""description"": ""Magnolia Roller Vixens is a community-wise sports initiative for adult females. Our purpose is for fun, friendly sports competition among like-minded women.  Magnolia Roller Vixens promotes and fosters the sport of women's flat track derby by facilitating the development of athletic ability, sportsw..."",
            ""properties"": [
                {
                    ""name"": ""Page"",
                    ""text"": ""2,717 like this""
                }
            ],
            ""icon"": ""https://fbstatic-a.akamaihd.net/rsrc.php/v2/yf/r/0HZPW6-lhQu.png""
        }
    }
 ]
}";

这篇关于反序列化动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆