0

I'm trying to deserialize json data formated in a way I haven't seen before. I'm using json.net and C#.

The class corresponding to the json should be like this:

class Example
{
   public Person[] data { get; set; }
}

class Person
{
    public string Id { get; set; }
    public string Nationality { get; set; }
    public string Name { get; set; }
} 

And this is how the json looks like:

{
   data: {
      "123": ["SWE", "Steve"],
      "221": ["USA", "Bob"],
      "245": ["CAN", "Susan"]
   }
}

Is it possible using attributes or do I have to do it all myself?

mason
  • 31,774
  • 10
  • 77
  • 121
Rios
  • 394
  • 3
  • 6
  • 3
    That's not valid JSON. Always [verify you have valid JSON](https://jsonlint.com/) first. – mason May 03 '17 at 13:03
  • So to clarify, I do not control the json data, It is as it is. And my question is, is it parsable? – Rios May 03 '17 at 13:05
  • To clarify, that's not JSON then. You should contact whoever provides that data and tell them they're not generating valid JSON. – mason May 03 '17 at 13:08
  • 3
    There is a way if you use Newtonsoft nuget library. By using this, you can cast your json to dictionary object and then Person object – Lali May 03 '17 at 13:10
  • Thanks for the answer mason. Then I can stop trying to parse it. – Rios May 03 '17 at 13:10
  • @Rios Are you sure you are not missing quotes around data like `"data" :` – Nkosi May 03 '17 at 13:18
  • Thanks Lali! That seems to be working. I found this answer that answered the same question I had. http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net – Rios May 03 '17 at 13:21

1 Answers1

-5

Your variables on the data object (123, 221, 245) would a type List<string>().

With JSON.NET, your schema needs to match your data; types and names.

EDIT: Looking at your Person object, your POCO classes don't seem correct. You would need a structure like this:

public Data data { get; set; }

where data is...

public class Data
{
    public List<string> 123 { get; set; }
    ...
}
Chris Bohatka
  • 363
  • 1
  • 4
  • 14