6

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

aaaayyyy和的值zzzz可以是任何字符串,并且可以有任意数量。

我需要提取所有的[aaaa|yyyy|zzz].categories.analysis值。也就是说,我需要以Dictionary<string, string>对象名称(例如AbbbbAcccc等)和 ID 结尾,忽略name字符串。

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

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

我已经接近了这样的事情:

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

但同样,aaaa可以是任何字符串,所以我不确定如何动态引用它。

4

1 回答 1

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);
}
于 2013-03-05T16:01:51.230 回答