I'm having a little trouble deserializing a JSON object when there's an empty/null property (using JSON.NET), and hoping someone can point me in the right direction. Below is a snippet of code I'm trying, and been testing at dotnetfiddle
Here's a sample of the JSON:
{
"`LCA0009": [],
"`LCA0001": {
"23225007190002": "1",
"23249206670003": "1",
"01365100070018": "5"
},
"`LCA0003": {
"23331406670018": "1",
"24942506670004": "1"
},
"`LCA0005": {
"01365100070018": "19"
}
}
I'm 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);
}
}
}
}
The "doesn't work" above is that I get this error:
Run-time exception (line 19): Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '`LCA0009', line 1, position 14.
How can I remove the properties that have a null/empty array?
Fundamentally, the JSON is inconsistent and the best answer is to make it consistent. Whoever is giving you that JSON needs to be told to fix it.
Until/unless you can do that, you can convert the "raw" version to Dictionary<string, Dictionary<string, int>>
yourself, but it's more work:
https://dotnetfiddle.net/zdeOOX
string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}";
// Convert it
var root = (JObject)JsonConvert.DeserializeObject(json);
var results = new Dictionary<string, Dictionary<string,int>>();
foreach (var entry in root)
{
var dict = new Dictionary<string,int>();
if (!(entry.Value is JArray))
{
foreach (var subentry in (JObject)entry.Value)
{
int v;
if (int.TryParse(((JValue)subentry.Value).ToString(), out v))
{
dict.Add(subentry.Key, v);
}
}
}
results.Add(entry.Key, dict);
}
// Results:
foreach (var name in results.Keys)
{
var entry = results[name];
Console.WriteLine(name + ":");
foreach (var entryKey in entry.Keys)
{
Console.WriteLine("- " + entryKey + ": " + entry[entryKey]);
}
}
I expect that can be made much more elegant with Linq.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With