I have a little problem deserializing a JSON object with an empty / null property (using JSON.NET) and in the hope that someone can point me in the right direction. Below is the code snippet I'm trying and tested on dotnetfiddle
Here is a sample JSON:
{
"`LCA0009": [],
"`LCA0001": {
"23225007190002": "1",
"23249206670003": "1",
"01365100070018": "5"
},
"`LCA0003": {
"23331406670018": "1",
"24942506670004": "1"
},
"`LCA0005": {
"01365100070018": "19"
}
}
I am trying to use this code:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}";
Console.WriteLine(json);
Console.WriteLine();
Console.WriteLine("This works");
var root = JsonConvert.DeserializeObject(json);
Console.WriteLine(root);
Console.WriteLine("This doesn't work");
var root2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(json);
Console.WriteLine(root2);
foreach (var locationKvp in root2)
{
foreach (var skuKvp in locationKvp.Value)
{
Console.WriteLine("location: " + locationKvp.Key + ", sku: " + skuKvp.Key + ", qty: " + skuKvp.Value);
}
}
}
}
"Doesn't work" above - I get this error:
( 19): JSON (, [1,2,3]) "System.Collections.Generic.Dictionary`2 [System.String, System.Int32] ' JSON (, {" name ":" value "}). , JSON JSON (, {" name ":" value"}) , (, ICollection, IList), List, JSON. JsonArrayAttribute , JSON. 'LCA0009', 1, 14.
/ ?