2

I'd like to deserialize this JSON as a C# class, but I need to define the class first:

{
"Prods": [
    {
        "ID": {
            "1001": [
                {
                    "Name": "Name1",
                    "Prod": "XXX"
                },
                {
                    "Name": "Name2",
                    "Prod": "XXXX"
                }
            ]
        }
    },
    {
        "ID": {
            "2001": [
                {
                    "Name": "Name3",
                    "Prod": "XXX-000"
                },
                {
                    "Name": "Name4",
                    "Prod": "XXXX-1111"
                }
            ]
        }
    }
]
}

I'd like to deserialize it into something like this, but nor sure if my JSON structure fits it:

public class Prod
{
    int CATID;
    Dictionary<string, string> prods = new Dictionary<string, string>();
}

public class Prods
{
   public List<Prod> Products {get;set;}
}

How should I change the JSON above to fit with my class below it, so I can use JsonConvert to deserialize into it?

Thanks!

A.G.
  • 2,089
  • 3
  • 30
  • 52
  • Have you checked out http://msdn.microsoft.com/en-us/library/bb412179.aspx ? http://stackoverflow.com/questions/9586585/convert-json-to-a-c-sharp-array has a similar problem to solve. – Warty Nov 01 '13 at 23:29
  • Your JSON has field names like "ID", "Name", "Prod", which means it won't convert to dictionaries in a single step. Your best bet is to deserialize into Lists<.> and then from there run .Select(...).ToDictionary(...). – Emanuel Nov 01 '13 at 23:33
  • Do you want your C# to match the JSON, or your JSON to match the C# code? Or are you open to changes in either? – Davin Tryon Nov 01 '13 at 23:34
  • Define custom types for *all* objects from JSON. – user2864740 Nov 01 '13 at 23:35
  • I want JSON to match the C#. – A.G. Nov 02 '13 at 00:41
  • Possible duplicate of [How to convert XML/JSON file to C# class?](http://stackoverflow.com/questions/19612215/how-to-convert-xml-json-file-to-c-sharp-class) – Liam May 16 '17 at 07:42

2 Answers2

3

To me your JSON just looks wrong as you don't always have 'consistent' name/value pairs. First I'd define my JSON like this

{
  "Prods": 
    [{
      "ID": "1001",
      "Items": 
        [{
          "Name": "Name1",
          "Prod": "XXX"
        },
        {
          "Name": "Name2",
          "Prod": "XXXX"
        }]        
    },
    {
      "ID": "2001", 
      "Items": 
        [{
          "Name": "Name3",
          "Prod": "XXX-000"
        },
        {
          "Name": "Name4",
          "Prod": "XXXX-1111"
        }]        
    }]
}

then I'd have the following classes to handle the data

public class Item {
  public string Name { get; set;}
  public string Prod { get; set;}
}

public class Product {
  public string ID { get; set;}
  public List<Item> Items { get; set; }
}

public class Products {
  public List<Product> Prods { get; set; }
}

then I'd use something like json.net to handle the serialization

Shaun Wilde
  • 8,228
  • 4
  • 36
  • 56
  • One question, when I deserialize in the class, would it be a one step like this: Jsonconvert... or do I have to convert the others too? – A.G. Nov 02 '13 at 00:47
  • you should only have to handle the top level entity i.e. `JsonConvert.DeserializeObject(jsondata);` – Shaun Wilde Nov 02 '13 at 00:50
1

In Visual Studio 2019, you can create a new class file, delete the class stub and use Edit->Paste Special->Paste JSON as Classes. This very quickly generates the classes you need to represent your JSON. If you don't like the way it comes out, you can adjust your JSON string and try again.

If you have some properties that don't show up in every instance of the JSON object, you can define JsonExtensionData to hold the additional properties

    [JsonExtensionData]
    public Dictionary<string, object> additionalJSON { get; set; } =
        new Dictionary<string, object>();
Denise Skidmore
  • 2,286
  • 22
  • 51