Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

If you're going to hardcode the reference to aaaa in your code, why not hardcode the class? You can use something like http://json2csharp.com/ to create C# objects from sample JSON, then you can use

var ajsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString)
share|improve this answer
    
My specific problem is that I can't hardcode aaaa, because it could be any string. –  Tom Redman Mar 5 '13 at 15:22
    
But your source code has "aaaa" hardcoded in it. If you were accessing it with code like var itemName = "aaaa"; /* change itemName based on some logic.. */ then ajsonObject[itemName].categories.analysis or similar, I'd understand better. You've put "aaaa" in your source code though, it requires recompiling to change -- so I don't get why you can't also put the object definition in your source code. –  gregmac Mar 5 '13 at 19:53
    
In the original question, you see I wrote The values for aaaa, yyyy, and zzzz can be any string and there can be any number of them. and also But again, aaaa can be any string, so I'm not sure how to reference that dynamically. My issue is that I needed to remove the hardcoding to aaaa and reference it/them dynamically. That was the whole issue. –  Tom Redman Mar 5 '13 at 20:00
up vote 2 down vote accepted

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.