Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize dynamically named JSON objects in C# (using JSON.Net or otherwise)

I have the following JSON:

{
"aaaa": {
    "name": "General Name",
    "product": "book",
    "host": "book.example.com",
    "chapters": {
        "bbbb": {
            "name": "Chapter 1",
            "page": "1",
            "end_page": "25"
        }
    },
    "categories" : {
        "analysis":{
            "Abbbb" : {
                    "name": "B Chapter",
                    "id" : "9001"
            },
            "Acccc" : {
                    "name": "C Chapter",
                    "id" : "9001"
            },
            "Adddd" : {
                    "name": "D Chapter",
                    "id" : "9001"
            },
            "Aeeee" : {
                    "name": "E Chapter",
                    "id" : "9001"
            },
            "Affff" : {
                    "name": "F Chapter",
                    "id" : "9001"
            },
            "Agggg" : {
                    "name": "G Chapter",
                    "id" : "9001"
            }
        },
        "sources":{
            "acks" : {
                    "name": "S. Spielberg",
                    "id" : "9001"
            }
        }
    }
}
"yyyy": {
    "name": "Y General Name",
    "product": "Y book",
    "host": "ybook.example.com",
    ...
}
"zzzz": {
    "name": "Z General Name",
    "product": "Z book",
    "host": "zbook.example.com",
    ...
}

The values for aaaa, yyyy, and zzzz can be any string and there can be any number of them.

I need to extract all of the [aaaa|yyyy|zzz].categories.analysis values. That is, I need to end up with a Dictionary<string, string> of the object name (e.g., Abbbb, Acccc, etc.) and the ID, ignoring the name string.

E.g., [Abbbb, 9001] [Acccc, 9001] [Adddd, 9001] ... [Zaaaa, 9001]

I've been at this for way too long and feel like I'm missing something obvious. I've tried JSON.net and native serialization. This is a trivial task in every other language I've used.

I've come close with something like this:

var ajsonObject = JsonConvert.DeserializeObject<dynamic>(jsonString);
var oasearch_categories = ajsonObject.aaaa.categories.analysis;

But again, aaaa can be any string, so I'm not sure how to reference that dynamically.

like image 614
Tom Redman Avatar asked Mar 05 '13 15:03

Tom Redman


1 Answers

Took a while, but I figured it out. My requirement changed slightly from the original question... My final result needed to a Dictionary of lists, so I'd end up with a dictionary like:

DICT[ {"9001", ["Abbbb", "Acccc", "Adddd", ...]}, {"9002", ["Zbbbb, Zdddd", ...]}, etc. ]
      | key |  |          value               |   | key |  |       value       |

This is the result:

Dictionary<string, List<string>> idsAndTheirNames = new Dictionary<string, List<string>>();

try
{
    var ajsonObject = JsonConvert.DeserializeObject<dynamic>(JSONstring);

    foreach (var child in ajsonObject.Children())
    {
        foreach (var product in child.Children())
        {
            var categories = product.categories.analysis;

            foreach (var category in categories.Children())
            {
                foreach (var subcat in category)
                {
                    List<string> name = idsAndTheirNames[(string)subcat.id]; //e.g., "9001"

                    if (name == null) name = new List<string>();

                    name.Add(category.Name); //e.g., "Abbbb"
                    idsAndTheirNames[(string)subcat.id] = name; //"9001" -> ["Abbbb", "Acccc", etc.]

                    System.Diagnostics.Debug.WriteLine((string)category.Name);  //"Abbbb"
                    System.Diagnostics.Debug.WriteLine((string)subcat.name);    //"Chapter B"
                    System.Diagnostics.Debug.WriteLine((string)subcat.id);      //"9001"
                }
            }
        }
    }
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("JSON ERROR: " + ex.Message);
}
like image 75
Tom Redman Avatar answered Oct 14 '22 09:10

Tom Redman