-1

I want to scrape chrome bookmarks using json object. What I want to do is get all bookmarks from 1 folder. That is the structure of the json:

{
   "checksum": "d499848083c2c2e3a38f547da4cbad7c",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "children": [ {
               "url": "https://www.example.com/"
            }, {
               "url": "https://www.another-example.com/"
            } ],
            "name": "foo",
         } ],
         "name": "Menu bookmarks",
      },
      "other": {
         "name": "Another bookmarks",
      },
      "synced": {
         "name": "Phone bookmarks",
      }
   },
   "version": 1
}

In this case I want to get urls from folder foo. I am using Json.NET to convert string into object. This code:

    string input = File.ReadAllText(bookmarksLocation);
    using (StringReader reader = new StringReader(input))
    using (JsonReader jsonReader = new JsonTextReader(reader)) {
        JsonSerializer serializer = new JsonSerializer();
        var o = (JToken)serializer.Deserialize(jsonReader);
        var allChildrens = o["roots"]["bookmark_bar"]["children"];

        var fooFolder = allChildrens.Where(x => x["name"].ToString() == "foo");

        foreach (var item in fooFolder["children"]) {
            Console.WriteLine(item["url"].ToString());
        }
        Console.ReadKey();
    }

Is giving me an error: Cannot apply indexing with [] to an expression of type 'IEnumerable<JToken>'

Can you tell me what I did wrong?

Brak Danych
  • 395
  • 1
  • 2
  • 7
  • Does https://stackoverflow.com/questions/21002297/getting-the-name-key-of-a-jtoken-with-json-net help? – mjwills Sep 02 '17 at 00:56

1 Answers1

1

there is 1 loop is missing:

var fooFolder = allChildrens.Where(x => x["name"].ToString() == "foo");
foreach (var folder in fooFolder)
{
    foreach (var item in folder["children"])
    {
        Console.WriteLine(item["url"].ToString());
    } 
}