JS从本地文件读取JSON对象

时间:2015-10-13 13:49:14

标签: javascript json intellij-idea

我有一个简单的JS表单,它接受一个搜索词和一个下拉选项,并根据这些参数返回JSON数据。我相信我遇到的问题是,即使intelliJ看到对另一个文件的引用,它也无法处理它(' Unexpected Token Error)。

jsFiddle JSON片段:

var songs = [
{"title":"12-Bar Original"                      , "writer":"Lennon, McCartney, Harrison and Starkey     ", "vocalist":"Instrumental                                 "},
{"title":"Across the Universe"                  , "writer":"Lennon                                      ", "vocalist":"Lennon                                       "},
{"title":"Act Naturally"                        , "writer":"Russell, Morrison                           ", "vocalist":"Starkey                                      "},
{"title":"Ain't She Sweet"                      , "writer":"Yellen, Ager                                ", "vocalist":"Lennon                                       "},
{"title":"All I've Got to Do"                   , "writer":"Lennon                                      ", "vocalist":"Lennon                                       "},
{"title":"All My Loving"                        , "writer":"McCartney                                   ", "vocalist":"McCartney                                    "},
{"title":"All Things Must Pass"                 , "writer":"Harrison    —   —                           ", "vocalist":"                                             "},
{"title":"All Together Now"                     , "writer":"McCartney, with Lennon                      ", "vocalist":"McCartney, with Lennon                       "},
{"title":"All You Need Is Love"                 , "writer":"Lennon                                      ", "vocalist":"Lennon                                       "},
{"title":"And I Love Her"                       , "writer":"McCartney, with Lennon                      ", "vocalist":"McCartney                                    "}];

对此的想法?我确定我错过了一些愚蠢的话。欣赏眼睛。谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为您的变量songs已经拥有有效的JSON数据,因此无需进行解析。如果您的变量持有JSON字符串,则需要JSON.parse。这就是JSON.parse抛出意外令牌错误的原因。您可以简单地删除JSON.parse并仅使用songs