这是我的 JSON 文件开头:
{ “cards” :
{
“suite”: “Spades”,
“value”: “Two”,
“int_value”: “2”
},
{
“suite”: “Spades”,
“value”: “Three”,
“int_value”: “3”
},
{
“suite”: “Spades”,
“value”: “Four”,
“int_value”: “4”
},
这是我的解析器:
JSONParser jsonParser = new JSONParser();
try {
// --------------------
// parse the JSON file
FileReader fileReader = new FileReader(JSONFILEPATH);
JSONObject jsonObject = (JSONObject) jsonParser.parse(fileReader);
JSONArray allCards = (JSONArray) jsonObject.get("cards");
我正在尝试使用 FileReader 以及 JSON 和 JSON-Simple 库来解析此 JSON 文件。我认为我的 JSON 文件的格式是正确的,但我不知道如何克服此错误:
Unexpected character (“) at position 2.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at Deck.main(Deck.java:67)
最佳答案
您的 JSON 不正确。请检查
应该是这样的
{ “cards” :
[
{
“suite”: “Spades”,
“value”: “Two”,
“int_value”: “2”
},
{
“suite”: “Spades”,
“value”: “Three”,
“int_value”: “3”
},
{
“suite”: “Spades”,
“value”: “Four”,
“int_value”: “4”
}
]
}
此外,所使用的引号应该""
,而不是使用“”
关于java - 位置 2 处出现意外字符 (") JSON 解析器 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475332/