Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing Chrome Bookmark JSON Data in C#

In response to a question I asked a few days ago, I'm attempting to stretch myself a little, and do something that I've not really focussed on much before. I've done some searching (both here, and in general), but can't find the answers (or even reasonable hints) to what I want to achieve (though, a few things come close-ish).

Basically, I'm trying to deserialize the data for the Google Chrome bookmarks file using the Json.NET library (though, if there's a better alternative, I'm all for that - the documentation for this library is a little confusing in places). I'm a little confused as to the next step to take, due primarily to being used to PHP's fantastic handling of JSON data (using json_decode()), allowing for a single function call, and then simple associative-array access.

The library (Json.NET) wants me to specify an Object type that it can deserialize the JSON data into, but I'm not really sure how to go about structuring such an Object, given the format of the Bookmarks file itself. The format is something along the lines of:

{
   "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
}

Now, in PHP, I'd be far more used to doing something along the lines of the following, to get the data I wanted, and ending up with 'Json.NET':

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

I can work out, simply enough, what objects to create to represent the data (something like a root object, then a bookmark list object, and finally an individual bookmark object) - but I'm really not sure as to how to implement them, and then get the library to deserialize into the relevant objects correctly.

Any advice that can be offered would be greatly appreciated.

like image 434
James B Avatar asked Mar 06 '09 14:03

James B


1 Answers

It is not necessary to declare a type that reflects the json structure:

    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);
        }
    }
like image 54
Darin Dimitrov Avatar answered Oct 16 '22 03:10

Darin Dimitrov