简体   繁体   English

调用外部文件时,json中的“意外令牌”

[英]'Unexpected Token' in json when calling an external file

I'm trying to implement backbone for a new project. 我正在尝试为新项目实施骨干。 Also installed are underscore, requirejs, jquery and bootstrap. 还安装了下划线,requirejs,jquery和bootstrap。 Overall it is going well. 总的来说,进展顺利。 I want to incorporate some static survey question data into one of my data models. 我想将一些静态调查问题数据合并到我的一个数据模型中。

{
    "defaultOptions": {
        "1": "Strongly Agree", 
        "2": "Somewhat Agree",
        "3": "Have Mixed Feelings",
        "4": "Somewhat Disagree",
        "5": "Strongly Disagree",
        "6": "Have No Opinion",
        "7": "Do Not Wish To Respond"
    },
    "questions": {
        "1": {
            "question": "Question 1",
            "options": {}
        },
        "2": {
            "question": "Question 2",
            "options": {}
        },
        "3": {
            "question": "Question 3",
            "options": {}
        },
        "4": {
            "question": "Question 4",
            "options": {}
        },
        "5": {
            "question": "Question 5",
            "options": {}
        }
    }
}

I don't currently have access to the data API I will ultimately be using to build this project, so I'm currently working off of some dummy data exported from the previous iteration of this project (It's for the US Elections cycle, so it happens yearly). 我目前无法访问最终将用于构建此项目的数据API,因此我目前正在处理从此项目的上一次迭代中导出的一些虚拟数据(这是针对美国选举周期,因此它发生在每年)。 I'm trying to pull some static data out of the content model I'm using and put it into a separate file so it's out of my way and I don't have to worry about inadvertently messing it up. 我正试图从我正在使用的内容模型中提取一些静态数据并将其放入一个单独的文件中,因此它不受我的影响,我不必担心无意中弄乱了它。 No big deal, right? 没什么大不了的吧? I'm not crossing any domains, it all lives on the same server. 我没有跨越任何域,它们都存在于同一台服务器上。 I have another data set doing the same thing. 我有另一个数据集做同样的事情。 I even ran the information through http://www.jsoneditoronline.org/ to make sure it was valid json. 我甚至通过http://www.jsoneditoronline.org/运行了这些信息,以确保它是有效的json。

So what's the problem? 所以有什么问题? I get this error in chrome "Uncaught SyntaxError: Unexpected token : on line 2" and this error in FireFox 'SyntaxError: missing ; 我在Chrome中遇到此错误“Uncaught SyntaxError:Unexpected token:on line 2”并且FireFox中出现此错误'SyntaxError:missing; before statement "defaultOptions":', with the indicator pointing towards the : after defaultOptions. 在语句“defaultOptions”:'之前,指示符指向:defaultOptions之后。

I really have no idea why this is happening. 我真的不知道为什么会这样。

I am calling two json files, one right after the other: 我正在调用两个json文件,一个接着另一个:

var survey = require('/elections/data/surveyQuestions.json');
var endorsements = require('/elections/data/endorsements.json');

The endorsements.json file (and anything else called with require) doesn't have any issues. endorsements.json文件(以及使用require调用的任何其他内容)没有任何问题。 Here it is for reference: 这里供参考:

[
    "Abortion Rights Council",
    "AFL-CIO",
    "AFSCME",
    "American Federation of Teachers",
    "Building and Construction Trades Council",
    "DFL Feminist Caucus",
    "DFL Party",
    "Education Minnesota",
    "Freedom Club",
    "GOP Feminist Caucus",
    "Grassroots Party",
    "Green Party of Minnesota",
    "Independence Party",
    "Libertarian Party of Minnesota",
    "MAPE",
    "Minnesota Citizens Concerned for Life",
    "Minnesota Police and Peace Officers Association",
    "National Association of Social Workers",
    "Republican Party of Minnesota",
    "Sierra Club",
    "Stonewall DFL",
    "TakeAction Minnesota",
    "Taxpayers League of Minnesota",
    "Teamsters DRIVE",
    "United Auto Workers"
]

Does anybody have any thoughts as to what I'm missing? 有没有人想过我错过了什么?

RequireJS is expecting an AMD module when you call require() . 当您调用require()时,RequireJS期待一个AMD模块。 You should use the RequireJS text plugin for loading text resources like JSON. 您应该使用RequireJS文本插件来加载JSON等文本资源。 Also look at this GitHub gist for a plugin to parse JSON specifically that's built on top of the text plugin. 另外看看这个GitHub的要点 ,一个插件来解析JSON,特别是它建立在文本插件之上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM