java - 位置 2 处出现意外字符 (") JSON 解析器 (Java)

标签 java json parsing

这是我的 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/

相关文章:

java - Ant jar 任务仅包含单个类文件

javascript - 如何按json属性排序

excel - Perl 'can' t 通过包 "worksheets"定位对象方法 "Spreadsheet::ParseExcel::Workbook"'

java - 方向不变(Actionbar 和 ICS)

java - Swingx组件使用方法 : jXSearchPanel with jTable

javascript - 尝试将 JSON 格式日期集合转换并显示为正常格式

python - 如何解析没有分隔符的元组?

c++ - 如何解析带引号字段中嵌入逗号的 CSV?

java - Jackson - 不要将@JsonProperty 与@JsonPOJOBuilder 或@JsonCreator ctor 一起使用

json - 使用 postgresql 9.4 访问 JSON 的更深层元素