在JSON中反序列化List中的List.Net wp8 c#

本文关键字:List Net wp8 中的 反序列化 JSON | 更新日期: 2023-09-27 18:12:53

我需要反序列化这个JSON:

{
"kind": "plus#activityFeed",
"etag": "'"JOoejvNXXEWjTp1RkvR4kuXnXWE/vHWQzjhUi3hCdDLiE71KtrP47bA'"",
"nextPageToken": "CAIQ-MGf9Z72tgIgAigC",
"title": "Google+ List of Activities for Collection PUBLIC",
"updated": "2013-05-02T01:13:01.244Z",
"items": [
    {
        "kind": "plus#activity",
        "etag": "'"JOoejvNXXEWjTp1RkvR4kuXnXWE/AgB1ExcMum9t0T-ruWqtunO3kS0'"",
        "title": "Gene gets creative - and slightly offensive - in a table-setting competition.",
        "published": "2013-05-02T01:13:01.244Z",
        "updated": "2013-05-02T01:13:01.244Z",
        "id": "z13dezeopsqzg3y4404chhh52xnhepqxxdo",
        "url": "link",
        "actor": {
            "id": "102458557561671030191",
            "displayName": "Bob's Burgers",
            "url": "link",
            "image": {
                "url": "link"
            }
        },
        "verb": "post",
        "object": {
            "objectType": "note",
            "content": "Gene gets creative - and slightly offensive - in a table-setting competition.",
            "url": "link",
            "replies": {
                "totalItems": 0,
                "selfLink": "link"
            },
            "plusoners": {
                "totalItems": 19,
                "selfLink": "link"
            },
            "resharers": {
                "totalItems": 2,
                "selfLink": "link"
            },
            "attachments": [
                {
                    "objectType": "article",
                    "displayName": "Table-Scaping from '"Boyz 4 Now'" | BOB'S BURGERS | ANIMATION on FOX",
                    "content": "",
                    "url": "link",
                    "image": {
                        "url": "link",
                        "type": "image/jpeg",
                        "height": 150,
                        "width": 150
                    },
                    "fullImage": {
                        "url": "link",
                        "type": "image/jpeg"
                    }
                }
            ]
        },
        "provider": {
            "title": "Google+"
        },
        "access": {
            "kind": "plus#acl",
            "description": "Public",
            "items": [
                {
                    "type": "public"
                }
            ]
        }
    },
    {
        "kind": "plus#activity",
        "etag": "'"JOoejvNXXEWjTp1RkvR4kuXnXWE/4ijLLkjecVl_dKuv7sJ7CCnuQnc'"",
        "title": "Did you recognize New Girl star Max Greenfield as the voice of '"Boyz 4 Now'" heartthrob, Boo Boo, on last...",
        "published": "2013-05-02T01:12:46.516Z",
        "updated": "2013-05-02T01:12:46.516Z",
        "id": "z122x1ejvrz2uj4s323ffdcrgvephrjeh",
        "url": "link",
        "actor": {
            "id": "102458557561671030191",
            "displayName": "Bob's Burgers",
            "url": "link",
            "image": {
                "url": "link"
            }
        },
        "verb": "post",
        "object": {
            "objectType": "note",
            "content": "Did you recognize New Girl star Max Greenfield as the voice of "Boyz 4 Now" heartthrob, Boo Boo, on last Sunday's episode of Bob's Burgers?",
            "url": "link",
            "replies": {
                "totalItems": 3,
                "selfLink": "link"
            },
            "plusoners": {
                "totalItems": 8,
                "selfLink": "link"
            },
            "resharers": {
                "totalItems": 0,
                "selfLink": "link"
            },
            "attachments": [
                {
                    "objectType": "article",
                    "displayName": "Exclusive: Bob's Burgers First Look: New Girl's Max Greenfield Is the Boyband-er of Your Dreams",
                    "content": "No one can play a lovable douchebag like Max Greenfield on New Girl, but did you also know he can...",
                    "url": "link",
                    "image": {
                        "url": "link",
                        "type": "image/jpeg",
                        "height": 150,
                        "width": 150
                    },
                    "fullImage": {
                        "url": "link",
                        "type": "image/jpeg"
                    }
                }
            ]
        },
        "provider": {
            "title": "Google+"
        },
        "access": {
            "kind": "plus#acl",
            "description": "Public",
            "items": [
                {
                    "type": "public"
                }
            ]
        }
    }
]
}

这是我用来反序列化的代码:

var rootObject = JsonConvert.DeserializeObject<Classes.activities.RootObject>(e.Result);
            foreach (var activity in rootObject.items)
            {
            }

这些是类:

 public class Image
        {
            public string url { get; set; }
        }
        public class Actor
        {
            public string id { get; set; }
            public string displayName { get; set; }
            public string url { get; set; }
            public Image image { get; set; }
        }
        public class Replies
        {
            public int totalItems { get; set; }
            public string selfLink { get; set; }
        }
        public class Plusoners
        {
            public int totalItems { get; set; }
            public string selfLink { get; set; }
        }
        public class Resharers
        {
            public int totalItems { get; set; }
            public string selfLink { get; set; }
        }
        public class Image2
        {
            public string url { get; set; }
            public string type { get; set; }
            public int height { get; set; }
            public int width { get; set; }
        }
        public class Embed
        {
            public string url { get; set; }
            public string type { get; set; }
        }
        public class FullImage
        {
            public string url { get; set; }
            public string type { get; set; }
        }
        public class Attachment
        {
            public string objectType { get; set; }
            public string displayName { get; set; }
            public string content { get; set; }
            public string url { get; set; }
            public Image2 image { get; set; }
            public Embed embed { get; set; }
            public FullImage fullImage { get; set; }
        }
        public class Object
        {
            public string objectType { get; set; }
            public string content { get; set; }
            public string url { get; set; }
            public Replies replies { get; set; }
            public Plusoners plusoners { get; set; }
            public Resharers resharers { get; set; }
            public List<Attachment> attachments { get; set; }
        }
        public class Provider
        {
            public string title { get; set; }
        }
        public class Item2
        {
            public string type { get; set; }
        }
        public class Access
        {
            public string kind { get; set; }
            public string description { get; set; }
            public List<Item2> items { get; set; }
        }
        public class Item
        {
            public string kind { get; set; }
            public string etag { get; set; }
            public string title { get; set; }
            public string published { get; set; }
            public string updated { get; set; }
            public string id { get; set; }
            public string url { get; set; }
            public Actor actor { get; set; }
            public string verb { get; set; }
            public Object @object { get; set; }
            public Provider provider { get; set; }
            public Access access { get; set; }
        }
        public class RootObject
        {
            public string kind { get; set; }
            public string etag { get; set; }
            public string nextPageToken { get; set; }
            public string title { get; set; }
            public string updated { get; set; }
            public List<Item> items { get; set; }
        }

现在的问题是,我不能访问任何项目后activity.@object.attachment

在JSON中反序列化List中的List.Net wp8 c#

您可以尝试将JsonProperty属性应用于.Net中保留关键字的属性(例如在您的情况下object)

[JsonObject]
public class Item
    {
        [JsonProperty(PropertyName = "kind")]
        public string kind { get; set; }
 ...
        [JsonProperty(PropertyName = "object")]
        public Object TheObject { get; set; }
 ...
    }