解析Chrome书签Json文件:Java

3

目前我正在使用NetBeans IDE。我尝试使用其他解决方案,但到目前为止都没有成功。

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

p/s:虽然在书签的名称中没有写明文件类型,但其内容已知为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)

正如您所看到的,只有校验和成功读取,但是根目录失败并产生了这些错误。

您还应该注意,我将一些代码放置在注释中,这些是我尝试过但仍然出现错误的内容。

我希望任何人能够帮助我使这些东西正常工作。 非常感谢您的帮助


Bookmarks.json 中的 roots 节点实际上是 JSONObject,因为它的值被包含在 {} 大括号中。只有当节点的值被包含在 [] 方括号中时,您才可以将其转换为 JSONArray。 - nazlo
3个回答

3
问题在于,您不能像这样将对象转换为数组:(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

@user7288944 你做了什么具体的更改来解决这个问题?我遇到了你提到的同样的错误。我尝试了Pavneet Singh建议的更改,但无法编译。 - L. D. James
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray at javaTools.JsonParser.main(JsonParser.java:27) - L. D. James
我在系统上有一个类似的问题。请给我一点时间找到它。 - L. D. James
@PavneetSingh 我犯了一个错误,编辑了错误的空格。我以为我在编辑屏幕。我以为我在编辑我的问题,但不小心编辑了你的答案。你可以通过拒绝编辑来修复它。但同时你可以看到我正在使用的代码。 - L. D. James
http://stackoverflow.com/questions/38601001/how-to-extract-each-element-bookmark-from-a-json-file-as-an-item-using-java - L. D. James
显示剩余5条评论

0

从 JSON 数据来看,“roots” 似乎不是一个数组,而是一个对象。然而,在“书签栏”下的“children” 是一个数组。


0

roots不是JSONArray,而是JSONObject。

所以你需要做的是

JSONObject lang = jsonObject.get("roots"); 

然后您需要循环遍历对象中的所有键,这应该是:

  • bookmark_bar
  • other
  • synced

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接