目前我正在使用netbeans IDE。我尝试使用其他解决方案,但到目前为止没有运气。
问题是,我在尝试从谷歌Chrome书签文件
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)
如您所见,只有 校验和 成功阅读,但 root 失败并发出这些错误。
您还应该注意到我将一些代码设置为 评论 ,这些是我尝试过的但仍然存在错误。
我希望有人能帮我把这些东西搞定。 非常感谢您的帮助
答案 0 :(得分:2)
问题是,您不能将对象强制转换为(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
答案 1 :(得分:0)
roots不是JSONArray而是JSONObject。
所以你需要做的是
JSONObject lang = jsonObject.get("roots");
然后你也要遍历对象中的所有键:
答案 2 :(得分:0)
在我看来,“roots”不是数组,而是查看JSON时的对象。 “bookmark_bar”下的“children”是一个数组但是