当前我正在使用NetBeans IDE。我尝试使用其他解决方案,但到目前为止尚无运气。
问题是,我在尝试读取JSON文件时会面临错误默认书签)
p/s:尽管没有书签名称编写的文件类型,但其内容被称为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:UsersAdminDocumentsNetBeansProjectsKeep-Itnbprojectbuild-impl.xml:1051: The following error occurred while executing this line:
C:UsersAdminDocumentsNetBeansProjectsKeep-Itnbprojectbuild-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
根不是jsonarray,而是jsonobject。
所以你要做的是
JSONObject lang = jsonObject.get("roots");
,然后您在对象中的所有键都过于循环:
- bookmark_bar
- 其他
- 同步
在我看来,"根"不是一个数组,而是看json时的对象。" bookmark_bar"下的"儿童"是一个数组,但是