'Parsing device listing from Urban Airship with JSON.Net

For the life of me, I can't figure out how to parse the collection of device_tokens out of this using JSON.Net. I can parse out the top level collection fine, but am bombing on parsing out the device tokens in any way shape or form. Anyone have any ideas?

    {
   "next_page": "https://go.urbanairship.com/api/device_tokens/?start=<MY_TOKEN>&limit=2",
   "device_tokens_count": 87,
   "device_tokens": [
      {
         "device_token": "<MY_TOKEN>",
         "active": false,
         "alias": null,
         "tags": []
      },
      {
         "device_token": "<MY_TOKEN>",
         "active": true,
         "alias": null,
         "tags": ["tag1", "tag2"]
      }
   ],
   "active_device_tokens_count": 37
}


Solution 1:[1]

Heres how you can do it using Json.NET

First create a class to represent a single device_token:

public class DeviceToken
{
    public string device_token { get; set; }
    public bool active { get; set; }
    public object alias { get; set; }
    public List<object> tags { get; set; }
}

Then using the JsonConvert class you can deserialize the json device_token array to a list of DeviceToken objects.

string json = "{\"next_page\": \"https://go.urbanairship.com/api/device_tokens/?start=07AAFE44CD82C2F4E3FBAB8962A95B95F90A54857FB8532A155DE3510B481C13&limit=2\",\"device_tokens_count\": 87,\"device_tokens\": [{\"device_token\": \"0101F9929660BAD9FFF31A0B5FA32620FA988507DFFA52BD6C1C1F4783EDA2DB\",\"active\": false,\"alias\": null,\"tags\": []},{\"device_token\": \"07AAFE44CD82C2F4E3FBAB8962A95B95F90A54857FB8532A155DE3510B481C13\",\"active\": true,\"alias\": null,\"tags\": [\"tag1\", \"tag2\"]      }],\"active_device_tokens_count\": 37}";

JObject obj = JObject.Parse(json);

var deviceTokens = JsonConvert.DeserializeObject<List<DeviceToken>>(obj["device_tokens"].ToString());

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gdp