Gmail 联系人/反序列化 json 包含电子邮件的对象
Gmail contacts/ deserialize json object containing emails
我想获取 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 反序列化为 class,我试图通过转换这个 json 来反序列化] 到 C# 但证明不成功。
让我知道可以做什么。
解决了我的问题,有时会出现像“$t”这样的实例或其中包含“$”的类似内容,当转换为 C# class 时会导致名称无效。要解决这个问题,请添加 dll 引用 'Newtonsoft.Json' 并使用命名空间作为 'using Newtonsoft.Json' 创建一个 class 并在每个 属性 中名称变得无效,如下所示;
public string __invalid_name__$t { get; set; }
添加 Json 属性 属性,如下所示,并根据您的喜好更改名称:
[JsonProperty("$t")]
public string NameOfMyChoosing { get; set; }
我想获取 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 反序列化为 class,我试图通过转换这个 json 来反序列化] 到 C# 但证明不成功。 让我知道可以做什么。
解决了我的问题,有时会出现像“$t”这样的实例或其中包含“$”的类似内容,当转换为 C# class 时会导致名称无效。要解决这个问题,请添加 dll 引用 'Newtonsoft.Json' 并使用命名空间作为 'using Newtonsoft.Json' 创建一个 class 并在每个 属性 中名称变得无效,如下所示;
public string __invalid_name__$t { get; set; }
添加 Json 属性 属性,如下所示,并根据您的喜好更改名称:
[JsonProperty("$t")]
public string NameOfMyChoosing { get; set; }