Gmail联系人/反序列化包含电子邮件的json对象

时间:2017-03-17 16:23:59

标签: c# json gmail

我想获得gmail联系人(实际上是电子邮件联系人),因此我在google开发者控制台上注册并获得了凭据,并使用访问令牌获取了联系人。范围:

https://www.google.com/m8/feeds/ + https://www.googleapis.com/auth/contacts.readonly

并请求uri:

https://www.google.com/m8/feeds/contacts/default/full?alt=json

给了我这个json的结果:

{
      "version": "1.0",
      "encoding": "UTF-8",
      "feed": {
        "xmlns": "http://www.w3.org/2005/Atom",
        "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
        "xmlns$batch": "http://schemas.google.com/gdata/batch",
        "xmlns$gd": "http://schemas.google.com/g/2005",
        "xmlns$gContact": "http://schemas.google.com/contact/2008",
        "id": {
          "$t": "xxx@test.ai"
        },
        "updated": {
          "$t": "2017-03-17T16:00:05.743Z"
        },
        "category": [
          {
            "scheme": "http://schemas.google.com/g/2005#kind",
            "term": "http://schemas.google.com/contact/2008#contact"
          }
        ],
        "title": {
          "type": "text",
          "$t": "xxx's Contacts"
        },
        "link": [
          {
            "rel": "alternate",
            "type": "text/html",
            "href": "http://www.google.com/"
          },
          {
            "rel": "http://schemas.google.com/g/2005#feed",
            "type": "application/atom+xml",
            "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full"
          },
          {
            "rel": "http://schemas.google.com/g/2005#post",
            "type": "application/atom+xml",
            "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full"
          },
          {
            "rel": "http://schemas.google.com/g/2005#batch",
            "type": "application/atom+xml",
            "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/batch"
          },
          {
            "rel": "self",
            "type": "application/atom+xml",
            "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full?alt=json&max-results=25"
          },
          {
            "rel": "next",
            "type": "application/atom+xml",
            "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full?alt=json&start-index=26&max-results=25"
          }
        ],
        "author": [
          {
            "name": {
              "$t": "xxx"
            },
            "email": {
              "$t": "xxx@test.ai"
            }
          }
        ],
        "generator": {
          "version": "1.0",
          "uri": "http://www.google.com/m8/feeds",
          "$t": "Contacts"
        },
        "openSearch$totalResults": {
          "$t": "32"
        },
        "openSearch$startIndex": {
          "$t": "1"
        },
        "openSearch$itemsPerPage": {
          "$t": "25"
        },
        "entry": [
          {
            "id": {
              "$t": "http://www.google.com/m8/feeds/contacts/xxx%40test.ai/base/ca1e0840ced5eb5"
            },
            "updated": {
              "$t": "2017-03-13T10:27:43.365Z"
            },
            "category": [
              {
                "scheme": "http://schemas.google.com/g/2005#kind",
                "term": "http://schemas.google.com/contact/2008#contact"
              }
            ],
            "title": {
              "type": "text",
              "$t": "Adam aaa"
            },
            "link": [
              {
                "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                "type": "image/*",
                "href": "https://www.google.com/m8/feeds/photos/media/xxx%40test.ai/ca1e0840ced5eb5/1B2M2Y8AsgTpgAmY7PhCfg"
              },
              {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/ca1e0840ced5eb5"
              },
              {
                "rel": "edit",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/ca1e0840ced5eb5/1489400863365001"
              }
            ],
            "gd$email": [
              {
                "rel": "http://schemas.google.com/g/2005#other",
                "address": "adam@test.ai",
                "primary": "true"
              }
            ]
          },
          {
            "id": {
              "$t": "http://www.google.com/m8/feeds/contacts/xxx%40test.ai/base/d06991a8a601600"
            },
            "updated": {
              "$t": "2017-03-13T10:27:43.365Z"
            },
            "category": [
              {
                "scheme": "http://schemas.google.com/g/2005#kind",
                "term": "http://schemas.google.com/contact/2008#contact"
              }
            ],
            "title": {
              "type": "text",
              "$t": "Bailey Bob"
            },
            "link": [
              {
                "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                "type": "image/*",
                "href": "https://www.google.com/m8/feeds/photos/media/xxx%40test.ai/d06991a8a601600/1B2M2Y8AsgTpgAmY7PhCfg"
              },
              {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/d06991a8a601600"
              },
              {
                "rel": "edit",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/d06991a8a601600/1489400863365001"
              }
            ],
            "gd$email": [
              {
                "rel": "http://schemas.google.com/g/2005#other",
                "address": "bailey@test.ai",
                "primary": "true"
              }
            ]
          },
          {
            "id": {
              "$t": "http://www.google.com/m8/feeds/contacts/xxx%40test.ai/base/e33f110891b26a2"
            },
            "updated": {
              "$t": "2017-03-13T10:27:43.365Z"
            },
            "category": [
              {
                "scheme": "http://schemas.google.com/g/2005#kind",
                "term": "http://schemas.google.com/contact/2008#contact"
              }
            ],
            "title": {
              "type": "text",
              "$t": "cathy Wales"
            },
            "link": [
              {
                "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                "type": "image/*",
                "href": "https://www.google.com/m8/feeds/photos/media/xxx%40test.ai/e33f110891b26a2/1B2M2Y8AsgTpgAmY7PhCfg"
              },
              {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/e33f110891b26a2"
              },
              {
                "rel": "edit",
                "type": "application/atom+xml",
                "href": "https://www.google.com/m8/feeds/contacts/xxx%40test.ai/full/e33f110891b26a2/1489400863365001"
              }
            ],
            "gd$email": [
              {
                "rel": "http://schemas.google.com/g/2005#other",
                "address": "cathy@test.ai",
                "primary": "true"
              }
            ]
          }
        ]
      }
    }

要么我需要一个更好的方法来从gmail中获取电子邮件联系人,或者我需要一种方法将此json反序列化为类,我尝试通过将此json转换为C#来反序列化,但证明不成功。 让我知道可以做些什么。

1 个答案:

答案 0 :(得分:0)

解决了我的问题,有时出现类似' $ t'或类似的东西' $'在其中,当转换为C#类时会导致名称无效。要解决此问题,请添加dll参考' Newtonsoft.Json'并使用Newtonsoft.Json'创建一个使用命名空间的类。并且在名称变为无效的每个财产中,如下所示;

public string __invalid_name__$t { get; set; }

添加如下所示的Json属性,并根据您的喜好更改名称:

[JsonProperty("$t")]
public string NameOfMyChoosing { get; set; }