【问题标题】:Unexpected character (") at position 2 JSON Parser (Java)位置 2 JSON 解析器 (Java) 的意外字符 (")
【发布时间】:2015-11-02 10:19:50
【问题描述】:

这是我的 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 库来解析这个 JSONfile。我认为我的 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)

【问题讨论】:

标签: java json parsing


【解决方案1】:

您的 JSON 不正确。请检查

应该是这样的

{ “cards” : 
    [
        {
            “suite”: “Spades”,
            “value”: “Two”,
            “int_value”: “2”
        },
        {
            “suite”: “Spades”,
            “value”: “Three”,
            “int_value”: “3”
        },
        {
            “suite”: “Spades”,
            “value”: “Four”,
            “int_value”: “4”
        }
    ]
}

使用的引号也应该"" 而不是“”

【讨论】:

  • 如何区分引号?我添加了方括号,但仍然没有用。
  • 您使用的引号是字符串引号和格式化的引号。使用评论中的引号" "
  • 谢谢,帮了大忙! :)
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 2020-10-16
  • 1970-01-01
相关资源
最近更新 更多