2013-05-08 65 views
0

我需要反序列化JSON這樣:反序列化一個列表,JSON.NET WP8 C#列表裏面

{ 
"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; } 
     } 

現在的問題是,我不能一後訪問任何項目ctivity。@ object.attachment

+0

-1沒有重播...真的嗎? – 2013-05-08 12:47:09

回答

1

你可以嘗試應用JsonProperty attribute到保留關鍵字在.net中(如object,你的情況)的屬性

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