2

我有一个独特的问题。我的程序正在从书签 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 语句复制/粘贴到我所说的“等等”中。ETC'?

谢谢!

编辑:这是o类:

JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(jsonReader);
4

1 回答 1

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);
    }
  }
}
于 2012-09-03T17:42:59.420 回答