I keep getting “Uncaught SyntaxError: Unexpected token o”

前端 未结 9 1467
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 01:46

I\'m trying to learn some html/css/javascript, so I\'m writing myself a teaching project.

The idea was to have some vocabulary contained in a json file which would t

相关标签:
9条回答
  • 2020-11-22 02:18
    const getCircularReplacer = () => {
                  const seen = new WeakSet();
                  return (key, value) => {
                    if (typeof value === "object" && value !== null) {
                      if (seen.has(value)) {
                        return;
                      }
                      seen.add(value);
                    }
                    return value;
                  };
                };
    JSON.stringify(tempActivity, getCircularReplacer());
    

    Where tempActivity is fething the data which produces the error "SyntaxError: Unexpected token o in JSON at position 1 - Stack Overflow"

    0 讨论(0)
  • 2020-11-22 02:19

    Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.

    Further explanation can be found in Aditya Mittal's answer.

    0 讨论(0)
  • 2020-11-22 02:29

    Another hints for Unexpected token errors. There are two major differences between javascript objects and json:

    1. json data must be always quoted with double quotes.
    2. keys must be quoted

    Correct JSON

     {
        "english": "bag",
        "kana": "kaban",
        "kanji": "K"
    }
    

    Error JSON 1

     {
        'english': 'bag',
        'kana': 'kaban',
        'kanji': 'K'
     }
    

    Error JSON 2

     {
        english: "bag",
        kana: "kaban",
        kanji: "K"
    }
    

    Remark

    This is not a direct answer for that question. But it's an answer for Unexpected token errors. So it may be help others who stumple upon that question.

    0 讨论(0)
提交回复
热议问题