Skip to content
Advertisement

Unexpected character (“) at position 2 JSON Parser (Java)

Here is my JSON File beginning:

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

And here is my Parser:

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");

I am trying to parse through this JSONfile using a FileReader and JSON and JSON-Simple libraries. I think my format of my JSON file is correct but I don’t know how to get past this error:

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)

Advertisement

Answer

Your JSON is not correct. Kindly check

It should be something like

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

also the quotes used should "" rather than using “”

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement