Java JSONArray 文本必须以 '[' at 1 [character 2 line 1] 开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39979590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A JSONArray text must start with '[' at 1 [character 2 line 1]
提问by Ghanem
I have a JSON file and i am trying to deal with but the following error is appears:
我有一个 JSON 文件,我正在尝试处理,但出现以下错误:
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] at org.json.JSONTokener.syntaxError(JSONTokener.java:433) at org.json.JSONObject.(JSONObject.java:195) at org.json.JSONObject.(JSONObject.java:319) at amazondataset.AmazonDataset.main(AmazonDataset.java:11) Java Result: 1
线程“main” org.json.JSONException 中的异常:JSONObject 文本必须以 '{' at 1 [character 2 line 1] at org.json.JSONTokener.syntaxError(JSONTokener.java:433) at org.json.JSONObject 开头.(JSONObject.java:195) 在 org.json.JSONObject.(JSONObject.java:319) 在 amazondataset.AmazonDataset.main(AmazonDataset.java:11) Java 结果:1
This is a sample of the file:
这是该文件的示例:
{ "reviewerID": "A2SUAM1J3GNN3B",
"asin": "0000013714",
"reviewerName": "J. McDonald",
"helpful": [2, 3],
"reviewText": "I bought this for my husband who plays the piano. He is having a wonderful time playing these old hymns. The music is at times hard to read because we think the book was published for singing from more than playing from. Great purchase though!",
"overall": 5.0,
"summary": "Heavenly Highway Hymns",
"unixReviewTime": 1252800000,
"reviewTime": "09 13, 2009"
}
and this is my code, simply:
这是我的代码,简单地说:
JSONObject ar = new JSONObject("E:\amazonDS.json");
for (int i = 0; i < ar.length(); i++) {
System.out.println( "Name: " + ar.getString("reviewerName").toString() );
}
采纳答案by Christian Kuetbach
You have to read the content of the file first, because the constructor of JSONArrayneeds the file-content and not the file-path.
您必须先读取文件的内容,因为JSONArray的构造函数需要文件内容而不是文件路径。
new JSONObject(new JSONTokener(new FileInputStream(new File("path"), "UTF-8")));
new JSONObject(new JSONTokener(new FileReader("path")));
updateYou should use a filereader or specify the charset for the FileInputStream
更新您应该使用文件阅读器或为 FileInputStream 指定字符集
回答by Krogh
JSON object look like this:
JSON 对象如下所示:
{ 'key1':'value1', 'key2':'value2' }
JSON array look like this:
JSON 数组如下所示:
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
If you output your data - you can valildate it here JSON lint
如果你输出你的数据 - 你可以在这里验证它JSON lint
EDIT:
Whoops, I was a little fast in my answer. Didn't see the java-tag.
This might help - TutorialsPoint- JSON with Java
编辑:
哎呀,我的回答有点快。没有看到java标签。
这可能会有所帮助 - TutorialsPoint- JSON with Java
EDITThanks Christian. {} added to the array
编辑谢谢基督徒。{} 添加到数组