我有一个文件sample.JSON,其中包含以下内容:
{ "resultset": {
"-count": "4904",
"-resultType": "campgrounds",
"result": [
{
"-availabilityStatus": "N",
"-contractID": "GA",
"-contractType": "STATE",
"-facilityID": "530145",
"-facilityName": "A. H. STEPHENS STATE HISTORIC PARK",
"-faciltyPhoto": "/webphotos/GA/pid530145/0/80x53.jpg",
"-latitude": "33.5633333",
"-longitude": "-82.8966667",
"-shortName": "C145",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "Y",
"-state": "GA"
},
{
"-availabilityStatus": "N",
"-contractID": "OH",
"-contractType": "STATE",
"-facilityID": "960023",
"-facilityName": "A.W. MARION STATE PARK",
"-faciltyPhoto": "/webphotos/OH/pid960023/0/80x53.jpg",
"-latitude": "39.6336111",
"-longitude": "-82.8747222",
"-shortName": "P023",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OH"
},
{
"-availabilityStatus": "N",
"-contractID": "NRSO",
"-contractType": "FEDERAL",
"-facilityID": "72346",
"-facilityName": "ACKER ROCK LOOKOUT",
"-faciltyPhoto": "/webphotos/NRSO/pid72346/0/80x53.jpg",
"-latitude": "43.0523056",
"-longitude": "-122.6456111",
"-shortName": "ARCL",
"-sitesWithAmps": "N",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OR"
}
] } }
这只是一个更大的JSON对象的示例,我想通过它并从每个数组对象中选择facilityName(不确定这是否是正确的术语),然后将每个facilityName写入新文件。
我知道我需要加载JSON对象,然后为对象的长度执行for循环。现在我只是想尝试加载sample.JSON文件。到目前为止,这是我的代码:
var data = require('./campSample.JSON');
var fs = require('fs');
console.log(JSON.parse(data));
尝试使用node运行它会不断给我一个SyntaxError:Unexpected token :,引用“resultset”中的冒号:{
我不知道为什么会这样。我使用JSON验证器来确认JSON是好的。为什么告诉我这种语法错了?
答案 0 :(得分:1)
require()
已经解析了您的JSON。
你不需要做任何事情。
答案 1 :(得分:1)
将文件名中的.JSON
更改为小写。
require()
special code将.json
文件解析为JSON而不是Javascript,但这是区分大小写的。