Disinfect a JSON object with an empty array

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.

/ ?

+4
1

, JSON , - . , , JSON , .

, , "" Dictionary<string, Dictionary<string, int>> , :

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]);
    }
}

, Linq .

+3

Source: https://habr.com/ru/post/1628448/


All Articles