java - 解析 Chrome 书签 Json 文件 : Java

标签 java json google-chrome parsing netbeans

目前我正在使用 netbeans IDE。我尝试使用其他解决方案,但到目前为止运气不佳。

问题是,当我尝试从 google Chrome 书签文件 读取 Json 文件时遇到错误 (C:\Users\Admin\AppData\Local\Google\Chrome\User Data\默认\书签)

p/s: Bookmarks虽然没有写文件类型,但是其内容已经被称为JSON

这是 Bookmarks.json 中的内容:

{
   "checksum": "20fdfad51db6d3199f8a09c3220dd93b",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13124893413824227",
            "id": "6",
            "name": "YouTube",
            "type": "url",
            "url": "https://www.youtube.com/"
         }, {
            "date_added": "13124893435163243",
            "id": "7",
            "name": "Welcome to Facebook",
            "type": "url",
            "url": "https://www.facebook.com/"
         } ],
         "date_added": "13124893381424539",
         "date_modified": "13124893435163243",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [  ],
         "date_added": "13124893381424547",
         "date_modified": "0",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13124893381424550",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

这是我的代码 (JsonParser.java):

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonParser{
    private static String jsonFile = "C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks";

    public static void main (String[] args) {

        try {
            FileReader reader = new FileReader (jsonFile); //access the file
            JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);

                      String c =(String) jsonObject.get("checksum"); //place
            //        String r =(String) jsonObject.get("roots"); //place

           //   String r =(String) jsonObject.get("children"); //place


                        System.out.println("check: " + c);
                        //System.out.println("roots: " + r);
                       JSONArray lang = (JSONArray) jsonObject.get("roots"); 
            for (int i=0; i<lang.size(); i++) {
            System.out.println ("Url Name : " + lang.get(i)+"\n");
            }       //data in the array
            }  catch (FileNotFoundException e) {
                                e.printStackTrace();
                    } catch (IOException e) {
                                e.printStackTrace();
                    } catch (ParseException e) {
                                e.printStackTrace();
                    }
    }
}

出于某种原因,当我运行代码时,这些是我遇到的错误:

check: 4d55f8a0888f7dd918a702eda2821ccd
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
at JsonParser.main(JsonParser.java:28)
C:\Users\Admin\Documents\NetBeansProjects\Keep-It\nbproject\build-impl.xml:1051: The following error occurred while executing this line:
C:\Users\Admin\Documents\NetBeansProjects\Keep-It\nbproject\build-impl.xml:805: Java returned: 1
BUILD FAILED (total time: 2 seconds)

如您所见,只有 checksum 被读取成功,但 roots 失败并放弃这些错误。

您还应该注意到,我将一些代码作为注释,这些是我尝试过但仍然出现错误的地方。

我希望任何人都可以帮助我让这些东西工作。 非常感谢您的帮助

最佳答案

问题是,您不能将对象转换为数组,如 (JSONArray) jsonObject.get("roots"); 您必须遵循结构,以便根据对象和数组进行解析,如下所示

 JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);
 String checksum =jsonObject.optString("checksum");

 //  get root object
 JSONObject root = jsonObject.getJSONObject("roots");

 //  get root bookmarks object from root
 JSONObject bookmarks = root.getJSONObject("bookmark_bar");

 //  get root children array from bookmarks 
 JSONArray  childrens = bookmarks.getJSONArray("children");

 JSONObject temp ;
  for (int i=0; i<childrens.size(); i++) {
      // get object using index from childrens array
      temp = childrens.getJSONObject(i);

      // get url
      String url = temp.optString("url");
  }

按照你的结构

JObject => root 
               root  JSONObject has : bookmark_bar JSONObject 
               bookmark_bar JSONObject has  : children JSONArray
               children JSONArray has JSONObject which further has String: url

关于java - 解析 Chrome 书签 Json 文件 : Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40887009/

相关文章:

java - 字符串将特定数量的单词打印到新行中

JavaEE :links to learn JBoss/EJB and JMS

html - 从 Chrome 迁移到 IE11 的主要样式问题

java - Stuckup with - HikariCP : JZ0C0: Connection is already closed (Sybase DB with SpringBoot v2. 1.7)

java - 在框架中的面板中绘图,我有标题,但冒号到目前为止不起作用

python - 'str' 对象没有属性 '__dict__'

ruby - 在 Ruby 中将字符串转换为 JSON

带有 json 的 php web 服务

CSS 的 JQuery 属性更改在 Chrome 中不起作用

google-chrome - 为什么 Chrome 一直在控制台中显示 "Site cannot be installed: the page is not served from a secure origin"?