JSON.parse()给出错误。需要解决

时间:2019-05-16 15:52:51

标签: javascript json

我需要使用JSON.parse()将字符串解析为JSON对象,并出现一些错误。字符串格式正确。

我尝试了其他方法,但失败了。

这是字符串:

[
  {
  "type": "FeatureCollection",
  "name": "jafvert_march_samples",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
  { "type": "Feature", "properties": { "Id": 0, "Name": "A1", "Descript": "upstream Sumbay Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.36309667, -16.06520167 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A2", "Descript": "under Bajo Grau Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.53931667, -16.39373 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A3", "Descript": "close to San Martin Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.54518, -16.40625333 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A4", "Descript": "under Tiabaya Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.59722833, -16.45542333 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A5", "Descript": "upstream Uchumayo Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.673785, -16.42648667 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A6", "Descript": "100 m downstream Sta. Rosa Bridge" }, "geometry": { "type": "Point", "coordinates": [ -71.69157667, -17.02936667 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A7", "Descript": "close to Freira Bridge, side Cocochacra town" }, "geometry": { "type": "Point", "coordinates": [ -71.77515167, -17.13146833 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A8", "Descript": "before Quilca bridge turned left, 500 m along left bank" }, "geometry": { "type": "Point", "coordinates": [ -72.405225, -16.69073667 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A9", "Descript": "Gravel road parallel to the river, Puchun town, turn left at church Sta  Rosa " }, "geometry": { "type": "Point", "coordinates": [ -72.75407833, -16.61971667 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A10", "Descript": "directly under the Siguas Bridge" }, "geometry": { "type": "Point", "coordinates": [ -72.12923167, -16.34259833 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A11", "Descript": 'Pedregal town, Pond "Junta de Usuarios Pampa Baja", inflow to storage pond at flume' }, "geometry": { "type": "Point", "coordinates": [ -72.19501, -16.387625 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A12", "Descript": 'Pedregal town, Pond "Junta de Usuarios Pampa Baja", exit well from storage pond' }, "geometry": { "type": "Point", "coordinates": [ -72.19616667, -16.38854667 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A13", "Descript": "Punta Colorada Bridge, from top of the bridge, closer to right bank" }, "geometry": { "type": "Point", "coordinates": [ -72.45209833, -16.27445167 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A14", "Descript": "Aplao Bridge, at top left corner of the bridge (direction from Aplao village)" }, "geometry": { "type": "Point", "coordinates": [ -72.48210333, -16.077535 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A15", "Descript": "Acoy villadge, hanging bridge / hydrological tsunami  station" }, "geometry": { "type": "Point", "coordinates": [ -72.483685, -16.00815333 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A16", "Descript": "Under the bridge, North of Acoy, slightly North from the sampling point 15" }, "geometry": { "type": "Point", "coordinates": [ -72.476565, -16.00008167 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A17", "Descript": 'Under Oconia Bridge, next to red cabin "Ministry of Defense"' }, "geometry": { "type": "Point", "coordinates": [ -73.117125, -16.42443 ] } },
  { "type": "Feature", "properties": { "Id": 0, "Name": "A18", "Descript": "Under Vitor Bridge, in La Joya region" }, "geometry": { "type": "Point", "coordinates": [ -71.92866667, -16.46566167 ] } },
  ]
  }
]

使用JSON.parse()

这是错误消息:

  

SyntaxError:JSON中位置2282上的意外令牌'       在JSON.parse       在eval:1:6       在评估       在o。

1 个答案:

答案 0 :(得分:1)

我看到“ Descript”的值不在双引号中,而是单引号,这使其成为无效的JSON。此外,您的双引号里面也应转义。我个人喜欢https://jsonformatter.curiousconcept.com/来验证我的JSON。这是有效的JSON:

db.collection('users').doc(uid).onSnapshot({ includeMetadataChanges: true }, (snap) => {
   if (!snap.metadata.fromCache) {
        const user = snap.data();
        // Code here
    }
})