Results 1 to 3 of 3

Thread: Help parsing json based chromium bookmarks files

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    42
    Thanked 8 Times in 8 Posts

    Default Help parsing json based chromium bookmarks files

    Hi, I'm trying to retrieve bookmarks from chrome/chromium; these are stored in a plain text file using json format. Have made some experiments with qjson but the best I got is the first level bookmarks.

    Here a sample file: http://paste.kde.org/2933/

    This is my (very ugly) code:

    Qt Code:
    1. QJson::Parser parser;
    2. bool ok;
    3.  
    4. QVariantMap result = parser.parse(&file, &ok).toMap();
    5. if (!ok) {
    6. ui->plainTextEdit->appendPlainText("An error occurred during parsing");
    7. return;
    8. }
    9.  
    10. QVariantMap rootsMap = result["roots"].toMap();
    11. QMapIterator<QString, QVariant> i(rootsMap);
    12.  
    13. while (i.hasNext()) {
    14. i.next();
    15. QVariantMap bookmarkbarMap = rootsMap[i.key()].toMap();
    16. QVariantList childrenMap = bookmarkbarMap["children"].toList();
    17.  
    18. for(int i = 0; i < childrenMap.size(); i++) {
    19. QVariantMap nestedMap = childrenMap.at(i).toMap();
    20. if(nestedMap["type"].toString()=="url") {
    21. qDebug() << nestedMap["name"].toString();
    22. qDebug() << nestedMap["url"].toString();
    23. qDebug() << "\n";
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Have tryed to use only QT as explained here (QScriptEngine/QScriptValue) but the examples there are too simple.

    Thanks
    Giuseppe CalÃ

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Help parsing json based chromium bookmarks files

    Hi,
    So does the code above work as expected ? And what have you tried to get the sublevel bookmarks ?

    If you include <QDebug> you can do something like this :
    Qt Code:
    1. qDebug() << nestedMap ;
    To copy to clipboard, switch view to plain text mode 
    This will make more clear what kind of data is stored in the variants produced by QJson.

    Best regards,
    Marc

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    42
    Thanked 8 Times in 8 Posts

    Default Re: Help parsing json based chromium bookmarks files

    Solved. If interested, below the code:

    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. QFile file(QDir::homePath() + "/.config/chromium/Default/Bookmarks");
    4. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    5. return;
    6.  
    7. QJson::Parser parser;
    8. bool ok;
    9.  
    10. QVariantMap result = parser.parse(&file, &ok).toMap();
    11. file.close();
    12. if (!ok) {
    13. ui->plainTextEdit->appendPlainText("An error occurred during parsing");
    14. return;
    15. }
    16.  
    17. QVariantMap rootsMap = result["roots"].toMap();
    18. QMapIterator<QString, QVariant> i(rootsMap);
    19.  
    20. while (i.hasNext()) {
    21. i.next();
    22. QVariantMap map = rootsMap[i.key()].toMap();
    23. getChildren(map);
    24. }
    25. }
    26.  
    27. void Dialog::getChildren(const QVariantMap & map) {
    28. QVariantList childrenMap = map["children"].toList();
    29.  
    30. for(int i = 0; i < childrenMap.size(); i++) {
    31. QVariantMap nestedMap = childrenMap.at(i).toMap();
    32. if(nestedMap["type"].toString()=="url") {
    33. m_name = nestedMap["name"].toString();
    34. m_url = nestedMap["url"].toString();
    35. // do something...
    36. }
    37.  
    38. if(nestedMap.contains("children"))
    39. getChildren(nestedMap);
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

Similar Threads

  1. JSON - parsing (arrays)
    By Tomasz in forum Newbie
    Replies: 2
    Last Post: 29th December 2010, 14:08
  2. Build Chromium On Linux By QT SDK ?
    By Thành Viên Mới in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2010, 06:24
  3. parsing JSON format
    By Raajesh in forum Qt Programming
    Replies: 5
    Last Post: 12th September 2010, 00:40
  4. Parsing Files
    By JayJay in forum Qt Programming
    Replies: 13
    Last Post: 20th August 2007, 20:01
  5. Parsing of Text files in Qt
    By shailesh in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2006, 16:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.