java 不断收到此错误“位置 1 处出现意外字符 ( )”。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27247005/
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
Keep getting this error "Unexpected character ( ) at position 1."
提问by dale
I'm having this issue where I'm keep getting this wierd error when I run this simple program at commandline
我遇到了这个问题,当我在命令行运行这个简单的程序时,我不断收到这个奇怪的错误
Here's the Json file
这是 Json 文件
{"count":"21740"}
{"count":"21740"}
Here's the code
这是代码
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 EarthQuake {
private static final String filePath = "data.json";
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
// get a String from the JSON object
String count = (String) jsonObject.get("count");
System.out.println("The count is: " + count);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
But I always get this error
但我总是收到这个错误
"C:\>java -classpath .;json-simple-1.1.1.jar test
Unexpected character ( ) at position 1.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
at test.main(test.java:22)"
I've validated the json file at http://jsonlint.com/
我已经在http://jsonlint.com/验证了 json 文件
I don't know what I've done wrong here, please help!
我不知道我在这里做错了什么,请帮忙!
回答by Jerome Anthony
I created a file and pasted {"count":"21740"}
in that file and called it test.json. I was able to run the code without an error.
我创建了一个文件并将其粘贴{"count":"21740"}
到该文件中,并将其命名为 test.json。我能够在没有错误的情况下运行代码。
But to debug your issue, can you run your code with the json string passed to the parser directly like below;
但是要调试您的问题,您是否可以使用直接传递给解析器的 json 字符串运行您的代码,如下所示;
JSONObject jsonObject = (JSONObject) jsonParser.parse("{\"count\":\"21740\"}");