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

C# Gmail联系人/反序列化包含电子邮件的json对象,c#,json,gmail,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结果: {

我想获得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#来反序列化,但没有成功。
让我知道可以做什么。

解决了我的问题,有时会出现诸如“$t”或类似的实例,其中包含“$”,当转换为C#类时,会导致无效名称。要解决此问题,请添加dll引用“Newtonsoft.Json”,并在名称无效的每个属性中创建一个使用名称空间为“using Newtonsoft.Json”的类,如下所示

public string __invalid_name__$t { get; set; }
添加Json属性,如下所示,并根据您的喜好更改名称:

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

我试图通过将此json转换为C来反序列化,但没有成功。-你试了什么?出了什么问题?像
“$t”
这样的属性名是否会导致问题?另外,我觉得JSON可能是从XML转换而来的。你能访问XML吗?解析会更容易吗?是的,$t变成了“\u无效的\u名称\u$t”,因此我无法转换。我也有xml,但是如何解析“var response=client.GetAsync(GetGoogleContactsUrl.Result)”中的响应是我无法理解的。因此,我修改了url,根据建议的google api文档添加了“?alt=json”,并得到了这个。