简体   繁体   中英

JS read in JSON object from local file

I have a simple JS form that takes a search term and a drop down option and returns JSON data based on those parameters. I believe the issue I'm running into is that even though intelliJ sees the reference to the other file, it's unable to process it ('Unexpected Token Error).

jsFiddle JSON Fragment:

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                                    "}];

Thoughts on this? I'm sure I'm missing something silly. Appreciate the eyes. Thanks!

That is because your variable songs already holds a valid JSON data and doesn't need to be parsed. JSON.parse would be required if your variable held a JSON string. That is why JSON.parse throws the unexpected token error. You can simply get rid of the JSON.parse and use just songs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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