views:

2097

answers:

1

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.

+7  A: 

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);
        }
    }
Darin Dimitrov
Won't compile. I think you meant to use JsonTextReader instead.
Adam Lassek
Additionally, I can't find anything about JavaScriptObject, what namespace is it part of? A quick Google turns up nothing specific.
James Burgess
Sorry, I was using an old version of the Json.NET assembly. I see that the API has changed since. I modified my post accordingly
Darin Dimitrov
This seems to do what I wanted, thanks a lot.
James Burgess
Fixed the code again, JObject seems to have become JToken
Luke
A simpler way of doing the same thing is this: JObject o = JObject.Parse(jsonText);
James Newton-King