使用具有特殊字符的json属性反序列化Json Restsharp

时间:2016-07-08 20:37:02

标签: c# .net json deserialization restsharp

我跟随谷歌联系人api v3返回的json(来自大型json响应的一部分)

"entry": [
   {
    "id": {
     "$t": "http://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/base/a769270d0bdc93"
    },
    "gd$etag": "\"R38-fDVSLit7I2A9WhFbEUkNQw0.\"",
    "updated": {
     "$t": "2013-09-02T18:44:56.154Z"
    },
    "app$edited": {
     "xmlns$app": "http://www.w3.org/2007/app",
     "$t": "2013-09-02T18:44:56.154Z"
    },
    "category": [
     {
      "scheme": "http://schemas.google.com/g/2005#kind",
      "term": "http://schemas.google.com/contact/2008#contact"
     }
    ],
    "title": {
     "$t": "Ruairi Browne"
    },
    "link": [
     {
      "rel": "http://schemas.google.com/contacts/2008/rel#photo",
      "type": "image/*",
      "href": "https://www.google.com/m8/feeds/photos/media/zahidmadeel%40gmail.com/a769270d0bdc93?v=3.0"
     },
     {
      "rel": "self",
      "type": "application/atom+xml",
      "href": "https://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/thin/a769270d0bdc93?v=3.0"
     },
     {
      "rel": "edit",
      "type": "application/atom+xml",
      "href": "https://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/thin/a769270d0bdc93?v=3.0"
     }
    ],
    "gd$name": {
     "gd$fullName": {
      "$t": "Ruairi Browne"
     },
     "gd$givenName": {
      "$t": "Ruairi"
     },
     "gd$familyName": {
      "$t": "Browne"
     }
    },
    "gd$email": [
     {
      "address": "ruairi.browne@mhlabs.net",
      "primary": "true",
      "rel": "http://schemas.google.com/g/2005#other"
     }
    ]
   },
   {
    "id": {
     "$t": "http://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/base/e268ff0bc63d0b"
    },
    "gd$etag": "\"QH89ezVSLyt7I2A9Wx5VE0gOTgY.\"",
    "updated": {
     "$t": "2010-10-06T08:48:41.163Z"
    },
    "app$edited": {
     "xmlns$app": "http://www.w3.org/2007/app",
     "$t": "2010-10-06T08:48:41.163Z"
    },
    "category": [
     {
      "scheme": "http://schemas.google.com/g/2005#kind",
      "term": "http://schemas.google.com/contact/2008#contact"
     }
    ],
    "title": {
     "$t": ""
    },
    "link": [
     {
      "rel": "http://schemas.google.com/contacts/2008/rel#photo",
      "type": "image/*",
      "href": "https://www.google.com/m8/feeds/photos/media/zahidmadeel%40gmail.com/e268ff0bc63d0b?v=3.0"
     },
     {
      "rel": "self",
      "type": "application/atom+xml",
      "href": "https://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/thin/e268ff0bc63d0b?v=3.0"
     },
     {
      "rel": "edit",
      "type": "application/atom+xml",
      "href": "https://www.google.com/m8/feeds/contacts/zahidmadeel%40gmail.com/thin/e268ff0bc63d0b?v=3.0"
     }
    ],
    "gd$email": [
     {
      "address": "salah@cirinpharma.com",
      "primary": "true",
      "rel": "http://schemas.google.com/g/2005#other"
     }
    ]
   }]

我创建了以下类,以便可以在.NET对象中反序列化此响应

public class Entry
    {

        [JsonProperty("id")]
        public  Id Id { get; set; }

        [JsonProperty("gd$etag")]
        public string GdEtag { get; set; }

        [JsonProperty("updated")]
        public  Updated Updated { get; set; }

        [JsonProperty("gd$name")]
        public GdName GdName { get; set; }

        [JsonProperty("gd$email")]
        public List<GdEmail> GdEmail { get; set; }

        [JsonProperty("gContact$website")]
        public List<GContactWebsite> GContactWebsite { get; set; }

        [JsonProperty("gContact$groupMembershipInfo")]
        public List<GContactGroupMembershipInfo> GContactGroupMembershipInfo { get; set; }
    }

    public class Feed
    {

        [JsonProperty("xmlns")]
        public string Xmlns { get; set; }

        [JsonProperty("xmlns$openSearch")]
        public string XmlnsOpenSearch { get; set; }

        [JsonProperty("xmlns$gContact")]
        public string XmlnsGContact { get; set; }

        [JsonProperty("xmlns$batch")]
        public string XmlnsBatch { get; set; }

        [JsonProperty("xmlns$gd")]
        public string XmlnsGd { get; set; }

        [JsonProperty("gd$etag")]
        public string GdEtag { get; set; }

        [JsonProperty("id")]
        public Id Id { get; set; }



        [JsonProperty("title")]
        public Title Title { get; set; }


        [JsonProperty("author")]
        public List<Author> Author { get; set; }

        [JsonProperty("generator")]
        public Generator Generator { get; set; }

        [JsonProperty("openSearch$totalResults")]
        public OpenSearchTotalResults OpenSearchTotalResults { get; set; }

        [JsonProperty("openSearch$startIndex")]
        public OpenSearchStartIndex OpenSearchStartIndex { get; set; }

        [JsonProperty("openSearch$itemsPerPage")]
        public OpenSearchItemsPerPage OpenSearchItemsPerPage { get; set; }

        [JsonProperty("entry")]
        public List<Entry> Entry { get; set; }
    }

    public class GoogleContactModel
    {

        [JsonProperty("version")]
        public string Version { get; set; }

        [JsonProperty("encoding")]
        public string Encoding { get; set; }

        [JsonProperty("feed")]
        public Feed Feed { get; set; }
    }

但数据未正确反序列化为c#对象。只要json属性名称中有$,就会出现问题。虽然,我已相应地使用JsonProperty属性修饰每个属性,但仍然将$的json属性转换为.NET对象中的相关数据。我怎么能做到这一点? 的修改 我自己并没有反应出反应。我正在使用rest sharp进行api调用,并通过以下代码行的restsharp对响应进行反序列化

response = restClient.Execute<GoogleContactModel>(restRequest);

我认为rest sharp在后端使用Json.NET进行序列化/反序列化

Edit2 我只是用Google搜索了一下,发现restsharp没有使用Json.NET进行序列化/ deseirialization。它有自己的属性DeserializeAs,等同于Json.Net中的JsonProperty。我将所有JsonProperty属性替换为DeserializeAs属性,但仍然无效。

0 个答案:

没有答案