解析Chrome书签Json文件:Java [英] Parsing Chrome Bookmarks Json file : Java

查看:206
本文介绍了解析Chrome书签Json文件:Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用netbeans IDE.我尝试使用其他解决方案,但到目前为止还算运气.

Currently I am using netbeans IDE. I tried using other solution, but to no luck so far.

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

Problem is, i am facing errors when trying to read the Json file from google Chrome bookmarks file (C:\Users\Admin\AppData\Local\Google\Chrome\User Data\Default\Bookmarks)

p/s:尽管没有以书签"的名义编写的文件类型,但其内容被称为JSON

p/s: although there is no file type written in the name of Bookmarks, its content have been known as JSON

这是Bookmarks.json中的内容:

This is the what inside the 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):

And here is my code (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();
                    }
    }
}

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

For some reason when I run the code these are the errors I got:

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)

如您所见,只有 校验和 成功读取,但是 失败并给出了这些错误.

As you can see, only checksum succeed in being read, but the roots failed and gave out these errors.

您还应该注意,我输入了一些代码作为 评论 ,这些是我尝试过的但仍然出现错误的代码.

You should also notice that there are some codes I put as comments, those are things i tried but still got the errors.

我希望有人能帮助我使这些工作正常进行. 非常感谢您的帮助

I hope anyone can help me to get these things working. Thank you very much for helping

推荐答案

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

Issue is , you cannot cast object to array like (JSONArray) jsonObject.get("roots"); you have to follow the structure so parse according to object and array as shown below

 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

这篇关于解析Chrome书签Json文件:Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆