未捕获(承诺)SyntaxError:访存函数中出现意外令牌' [英] Uncaught (in promise) SyntaxError: Unexpected token ' in fetch function

查看:106
本文介绍了未捕获(承诺)SyntaxError:访存函数中出现意外令牌'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个JSON文件,其结构如下(我们将其称为info.json):

I have a couple JSON files that are structured like this (let's call this info.json):

{
  'data': {
    'title': 'Job',
    'company': 'Company',
    'past': [
      'fulltime': [
        'Former Company'
      ],
      'intern': [
        'Women & IT',
        'Priority 5'
      ]
    ],
    'hobbies': [
      'playing guitar',
      'singing karaoke',
      'playing Minecraft',
    ]
  }
}

在一个单独的JavaScript文件中,我有一个看起来像这样的函数:

And in a separate JavaScript file, I have a function that looks like this:

function getJSONInfo() {
  fetch('info.json').then(function(response) {
    return response.json();
  }).then(function(j) {
    console.log(j);
  });
}

运行getJSONInfo()时,我不断收到此错误:

And I keep getting this error when I run getJSONInfo():

Uncaught (in promise) SyntaxError: Unexpected token '

我想念什么?我在任何地方都没有流浪',所以我不确定这是怎么回事.

What am I missing? I don't have a stray ' anywhere so I'm not sure what's wrong.

推荐答案

对于有效的json,您需要在属性中使用双引号.

You need to have double quotes for your attributes for valid json.

您可以使用json验证程序,例如 http://jsonlint.com/来检查语法是否正确.

You can use json validators such as http://jsonlint.com/ to check if your syntax is correct.

而且,正如shayanypn指出的那样,过去"应该是对象,而不是数组.您试图将过去"定义为对象文字,但是使用方括号表示数组.

Also, as shayanypn pointed out, "past" should be an object, rather than an array. You are trying to define "past" as an object literal but are using square brackets to denote an array.

这篇关于未捕获(承诺)SyntaxError:访存函数中出现意外令牌'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆