7

在回答我几天前提出的一个问题时,我试图让自己稍微舒展一下,做一些我以前没有真正关注过的事情。我已经进行了一些搜索(在这里和一般情况下),但是找不到我想要实现的答案(甚至是合理的提示)(尽管有些事情接近了)。

基本上,我正在尝试使用Json.NET 库对 Google Chrome 书签文件的数据进行反序列化(不过,如果有更好的选择,我完全赞成 - 这个库的文档在某些地方有点令人困惑) . 对于下一步要采取的措施,我有点困惑,主要是因为习惯于 PHP 对 JSON 数据的出色处理(使用json_decode()),允许单个函数调用,然后是简单的关联数组访问。

库(Json.NET)希望我指定一个对象类型,它可以将 JSON 数据反序列化为,但考虑到书签文件本身的格式,我不确定如何构建这样的对象。格式大致如下:

{
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "children": [ {
               "date_added": "12880758517186875",
               "name": "Example URL",
               "type": "url",
               "url": "http://example.com"
            }, {
               "date_added": "12880290253039500",
               "name": "Another URL",
               "type": "url",
               "url": "http://example.org"
            } ],
        "date_added": "12880772259603750",
            "date_modified": "12880772452901500",
            "name": "Sample Folder",
            "type": "folder"
         }, {
            "date_added": "12880823826333250",
            "name": "Json.NET",
            "type": "url",
            "url": "http://james.newtonking.com/pages/json-net.aspx";
         } ],
         "date_added": "0",
         "date_modified": "12880823831234250",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [  ],
         "date_added": "0",
         "date_modified": "0",
         "name": "Other bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

现在,在 PHP 中,我更习惯于按照以下方式进行操作,以获取我想要的数据,并以“Json.NET”结束:

$data['roots']['bookmark_bar']['children'][0]['name'];

我可以很简单地计算出要创建哪些对象来表示数据(例如根对象,然后是书签列表对象,最后是单个书签对象)-但我真的不确定如何实现它们,然后让库正确反序列化为相关对象。

任何可以提供的建议将不胜感激。

4

1 回答 1

12

不必声明反映 json 结构的类型:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.IO;
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            string json = 
@"
{
   ""roots"": {
      ""bookmark_bar"": {
         ""children"": [ {
            ""children"": [ {
               ""date_added"": ""12880758517186875"",
               ""name"": ""Example URL"",
               ""type"": ""url"",
               ""url"": ""http://example.com""
            }, {
               ""date_added"": ""12880290253039500"",
               ""name"": ""Another URL"",
               ""type"": ""url"",
               ""url"": ""http://example.org""
            } ],
        ""date_added"": ""12880772259603750"",
            ""date_modified"": ""12880772452901500"",
            ""name"": ""Sample Folder"",
            ""type"": ""folder""
         }, {
            ""date_added"": ""12880823826333250"",
            ""name"": ""Json.NET"",
            ""type"": ""url"",
            ""url"": ""http://james.newtonking.com/pages/json-net.aspx""
         } ],
         ""date_added"": ""0"",
         ""date_modified"": ""12880823831234250"",
         ""name"": ""Bookmarks bar"",
         ""type"": ""folder""
      },
      ""other"": {
         ""children"": [  ],
         ""date_added"": ""0"",
         ""date_modified"": ""0"",
         ""name"": ""Other bookmarks"",
         ""type"": ""folder""
      }
   },
   ""version"": 1
}
";
        using (StringReader reader = new StringReader(json))
        using (JsonReader jsonReader = new JsonTextReader(reader))
        {
            JsonSerializer serializer = new JsonSerializer();
            var o = (JToken)serializer.Deserialize(jsonReader);
            var date_added = o["roots"]["bookmark_bar"]["children"][0]["date_added"];
            Console.WriteLine(date_added);
        }
    }
于 2009-03-06T15:18:49.733 回答