在C#中反序列化动态命名的JSON对象(使用JSON.Net或其他方式)

时间:2013-03-05 15:09:03

标签: c# .net json serialization json.net

我有以下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",
    ...
}

aaaayyyyzzzz的值可以是任意字符串,可以有任意数量的字符串。

我需要提取所有[aaaa|yyyy|zzz].categories.analysis值。也就是说,我最终需要{{1>} 对象名称(例如Dictionary<string, string>Abbbb等)和ID,忽略Acccc字符串。

如,     [Abbbb,9001]     [Acccc,9001]     [Adddd,9001]     ...     [Zaaaa,9001]

我已经这么久了,感觉我错过了一些明显的东西。我尝试过JSON.net和本机序列化。这是我用过的其他语言中的一项微不足道的任务。

我接近这样的事情:

name

但同样,var ajsonObject = JsonConvert.DeserializeObject<dynamic>(jsonString); var oasearch_categories = ajsonObject.aaaa.categories.analysis; 可以是任何字符串,所以我不确定如何动态引用它。

1 个答案:

答案 0 :(得分:2)

花了一段时间,但我明白了。我的要求从原来的问题略有改变......我的最终结果需要一个列表字典,所以我最终得到一个字典,如:

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

结果如下:

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