4

I am in a pickle. I can not find how to deserialize this json. Everything i've found online talks about how to handle a key/value where the value is a type not an object. In this case the value is an object that has additional key/values inside.

Additionally, the long guid like string that you see there after "territories", is never the same...

How do i deserialize this with JSON.NET? I would love to be able to get the ID and the Name.

Thank you for reading this

{
  "territories":{
    "6368e494-c47c-4678-89de-45f3bfc35247":{
      "id":"6368e494-c47c-4678-89de-45f3bfc35247",
      "name":"Atlanta Camera Operations",
      "center":[
        33748995,
        -84387983
      ],
      "timeZoneCode":"America/New_York",
      "languageCode":"en"
    },
    "4d998d4c-fcc6-4fdc-9e73-3f2e7515ee47":{
      "id":"4d998d4c-fcc6-4fdc-9e73-3f2e7515ee47",
      "name":"Atlanta Operations",
      "center":[
        33748995,
        -84387983
      ],
      "timeZoneCode":"America/New_York",
      "languageCode":"en"
    }
  }
}
Demarily
  • 65
  • 6
  • From my experience with JSON.Net this works out of the box, doesn't it? If you have the same structure in your classes in C#, if territories is a property which is an object. The problem I can see is that a guid is assigned an object? Is it suppose to be serialized to a guid? or maybe a property name? If it's suppose to be serialized like that you'd need to do a custom formatter and pass that into JSON.Net. did your code also generate this JSON or is it just being expected to use it? – Skintkingle May 19 '16 at 12:56
  • If you have an object that has the property `territories` that happens to be `Dictionary` then this should work OK. Assuming `Territory` has all the properties defined as outlined. – Berin Loritsch May 19 '16 at 13:02
  • I've tried several different ways to do this out of the box. First, i tried using JsonConvert.DeserializeObject(), then i tried JObject.Parse(). I've tried putting the result into a dynamic and a JObject. – Demarily May 19 '16 at 13:04
  • Is this JSON generated by you or not? because if you are, it's serializing it wrongly. and if you're not, then you will need to write a CustomFormatter to handle the not-a-property-name fields. – Skintkingle May 19 '16 at 13:08
  • @Skintkingle it's from an API. I'll look into CustomFormatter. Any suggestions? – Demarily May 19 '16 at 13:13
  • @Demarily you will want to roll one of these: http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm Implementing the ReadJSON part. You will want to write one for Deserializing a Territories object, and intelligently only read the guid and use it somewhere else, you can then allow JSON to deserialize the object assigned to the GUID and put it onto something else that's not a guid on your territories object. – Skintkingle May 19 '16 at 13:16

2 Answers2

3

Here's a class that will hold the territory info:

class Territory
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("center")]
    public IList<int> Center { get; set; }

    [JsonProperty("timeZoneCode")]
    public string TimeZoneCode { get; set; }

    [JsonProperty("languageCode")]
    public string LanguageCode { get; set; }
}

Then you can deserialize them one by one like this:

string myJsonText = "your json here";

var jsonObject = JObject.Parse(myJsonText);
var territoriesToken = jsonObject["territories"];

We loop through the children and deserialize them:

var territoriesList = new List<Territory>();
foreach (var child in territoriesToken.Children())
{
    var territory = child.First.ToObject<Territory>();
    territoriesList.Add(territory);
}

Or if you prefer LINQ one liners you can use this:

var territoriesList = territoriesToken.Children()
                      .Select(child => child.First.ToObject<Territory>())
                      .ToList();

You can then access the name and id like you would with any other C# object

territoriesList[0].Id
territoriesList[0].Name
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
-1

your json data structure is invalid it should be like that :

{
"territories": 
[
    {
        "id": "6368e494-c47c-4678-89de-45f3bfc35247",
        "name": "Atlanta Camera Operations",
        "center": [
            33748995, -84387983
        ],
        "timeZoneCode": "America/New_York",
        "languageCode": "en"
       }
       ,
       {
        "id": "4d998d4c-fcc6-4fdc-9e73-3f2e7515ee47",
        "name": "Atlanta Operations",
        "center": [
            33748995, -84387983
        ],
        "timeZoneCode": "America/New_York",
        "languageCode": "en"
    }
]

}

wich you can map to Territory class, meaning :

    public class Territory
{
    public GUID id { get; set; }
    public string name { get; set; }
    public List<int> center { get; set; }
    public string timeZoneCode { get; set; }
    public string languageCode { get; set; }
}

and deserialize using :

JsonConvert.DeserializeObject<List<Territory>>(your_json)
Kevorkian
  • 430
  • 2
  • 4
  • 14
  • Sadly, the JSON comes from an API. Me an a few other developers on my team have tried to bring this to the company's attention with several failures. – Demarily May 19 '16 at 13:09
  • 1
    "your json data structure is invalid" No it's not. His JSON is valid. – Nasreddine May 19 '16 at 13:27
  • please correct me, why it's valid ?, according to [JsonLint](http://jsonlint.com) it is not ! – Kevorkian May 19 '16 at 13:38