我有這個問題,即我不斷收到這個奇怪的錯誤,當我在命令行中運行這個簡單的程序繼續在位置1收到此錯誤「意外字符()」。
這裏的JSON文件
{"count":"21740"}
下面的代碼
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();
}
}
}
但我總是得到這個錯誤
"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)"
我驗證過的http://jsonlint.com/
JSON文件,我不知道我做了什麼錯在這裏,請大家幫忙!
在文件的開頭是否有一些不可見的字符? – Jesper 2014-12-02 10:25:30
不,這一切都在文件中 – dale 2014-12-02 10:28:25
請將文件上傳到公共文件共享服務中,以便我們檢查文件是否有問題。我知道你說沒關係,但誰知道。 – Pimgd 2014-12-02 10:32:32