2017-03-17 144 views
0

我想获得gmail联系人(实际上是电子邮件联系人),因此我在Google开发者控制台上注册并获取了凭据,并使用访问令牌获取了联系人。借助范围:Gmail联系人/反序列化包含电子邮件的json对象

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": "[email protected]" 
     }, 
     "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": "[email protected]" 
      } 
      } 
     ], 
     "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": "[email protected]", 
       "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": "[email protected]", 
       "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": "[email protected]", 
       "primary": "true" 
       } 
      ] 
      } 
     ] 
     } 
    } 

无论我需要一个更好的方式来获得电子邮件联系人的Gmail帐户或者我需要一种方法来反序列化这个JSON类,我试图通过转换这个JSON到C#反序列化,但证明不成功。 让我知道可以做些什么。

+0

*我想通过这个JSON转换为C#反序列化,但证明是不成功的* - 那你试试?什么地方出了错?是否有一些像''t“'这样的属性名称会导致问题?另外,我觉得JSON可能已经从XML转换而来。你可以访问XML吗?分析会更容易吗? – dbc

+0

是的,$ t变成'_invalid_name_ $ t',因此我无法转换。我也有xml,但是如何解析“var response = client.GetAsync(GetGoogleContactsUrl).Result;”中的响应是我无法弄清楚的。所以我通过添加“?alt = json”来改变网址,并根据google api文档提出了相应的建议。 –

回答

0

解决了我的问题,在有时带有'$ t'或类似'$'的类似实例时,它们在转换为C#类时导致名称无效。为了解决这个问题,添加dll参考'Newtonsoft.Json'并创建一个使用命名空间作为'使用Newtonsoft.Json'的类,并且在每个属性中名称变得像下面那样无效;

public string __invalid_name__$t { get; set; } 

添加Json的财产属性,像下面并更改名称,按您的喜好:

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