Search code examples
c#jsongmail

Gmail contacts/ deserialize json object containing emails


I wanted to get gmail contacts(email contacts actually), so I registered on google developer console and got the credentials, and using access token got the contacts. With scopes:

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

and request uri as :

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

which gave me this json result:

{
      "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"
              }
            ]
          }
        ]
      }
    }

Either I need a better way to get the email contacts out of gmail or I need a way to deserialize this json to class, I tried to deserialize by converting this json to C# but proved unsuccessful. Let me know what can be done.


Solution

  • Resolved my issue, at times when comes with instances like '$t' or something similar having '$' in it, which when converted into C# class results in invalid name. To resolve that, add dll reference 'Newtonsoft.Json' and create a class with using namespace as 'using Newtonsoft.Json' and in each property where name becomes invalid like below;

    public string __invalid_name__$t { get; set; }
    

    Add Json property attribute like below and change the name as per your liking:

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