我有一个独特的问题。我的程序从书签JSON文件中获取chrome的书签。我正在使用JSON.net dll。我的问题是,我需要将每个URL都放在正确的文件夹层次结构中,但是,您似乎可以创建一个无限量的文件夹来保存书签。以下是JSON无政府主义的示例:
{
"checksum": "c9e24e7fb3c6cb184fb776e32dd1004e",
"roots": {
"bookmark_bar": {
"children": [ {
{
"children": [ {
"children": [ {
"children": [ {
"children": [ {
"date_added": "12985566069697953",
"id": "225",
"name": "EvasiveURL",
"type": "url",
"url": "evasiveurl.com"
} ],
"date_added": "12991165233108137",
"date_modified": "12991165244676611",
"id": "374",
"name": "NestedFolder2",
"type": "folder"
} ],
"date_added": "12991165215985934",
"date_modified": "12991165215985934",
"id": "373",
"name": "NestedFolder",
"type": "folder"
} ],
"date_added": "12985566069695953",
"date_modified": "12991165219618934",
"id": "204",
"name": "Games",
"type": "folder"
}
这是我目前的代码:
for (int i = 0; o["roots"]["bookmark_bar"]["children"][i] != o["roots"]["bookmark_bar"]["children"].Last; i++)
{
var property = o["roots"]["bookmark_bar"]["children"][i]["url"];
var property2 = o["roots"]["bookmark_bar"]["children"][i]["children"];
Console.WriteLine(o["roots"]["bookmark_bar"]["children"][i]["type"]);
if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "url") {
Console.WriteLine("URL");
}
else if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "folder")
{
Console.WriteLine("FOLDER");
for (int ii = 0; o["roots"]["bookmark_bar"]["children"][i]["children"][ii] != o["roots"]["bookmark_bar"]["children"][i]["children"].Last; ii++)
{
property = o["roots"]["bookmark_bar"]["children"][i]["children"][ii];
// Console.WriteLine(property);
if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "url")
{
Console.WriteLine("URL");
}
else if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "folder")
{
for (int iii = 0; o["roots"]["bookmark_bar"]["children"][i]["children"][ii]["children"][iii] != o["roots"]["bookmark_bar"]["children"][i]["children"][ii]["children"].Last; iii++)
{
if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "url")
{
Console.WriteLine("URL");
}
else if (o["roots"]["bookmark_bar"]["children"][i]["type"].ToString() == "folder")
{
//etc. etc.
}
}
}
}
}
}
那么,我怎样才能重写这段代码,而不会将之前的for循环和If-Else语句复制/粘贴到我说的'等等。等'?
谢谢!
编辑:这是类o:
JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(jsonReader);
答案 0 :(得分:5)
这将是您的问题的解决方案。正如我所说,你处理孩子的递归。
private void processJsonInput()
{
var reader = new StreamReader(new FileStream("d:\\jsonfile.txt", FileMode.Open));
JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(new JsonTextReader(reader));
foreach (var child in o["roots"]["bookmark_bar"]["children"])
{
processChild(child);
}
}
private void processChild(JToken child)
{
if (child["type"].ToString() == "url")
{
Console.WriteLine("URL");
}
else if (child["type"].ToString() == "folder")
{
Console.WriteLine("FOLDER");
// process sub childrens in the folder
foreach (var subChild in child["children"])
{
processChild(subChild);
}
}
}