使用双引号内的双引号从API响应中解析JSON

时间:2014-01-24 18:19:21

标签: javascript json

唉。所以noobish,但我似乎无法在Javascript中将此字符串解析为有效的JSON。我该怎么办?这些数据来自Foursquare签到。

编辑:这是我想要获得的图片,“checkin”属性 enter image description here

"{"id":"52e2ac6511d28d5cd63ee41f","createdAt":1390586981,"type":"checkin","shout":"Vd","timeZone":"America\/Denver","timeZoneOffset":-420,"user":{"id":"76097213","firstName":"SpencerTesting","lastName":"GardnerTesting","gender":"none","relationship":"self","photo":"https:\/\/foursquare.com\/img\/blank_boy.png","tips":{"count":0},"lists":{"groups":[{"type":"created","count":1,"items":[]}]},"homeCity":"Pleasant Grove, UT","bio":"","contact":{"email":"email@email.com"}},"venue":{"id":"4b83ec97f964a520d81631e3","name":"Joseph Smith Building","contact":{},"location":{"address":"770 E University Pkwy","crossStreet":"at BYU","lat":40.245901424248,"lng":-111.65174603462219,"postalCode":"84604","cc":"US","city":"Provo","state":"UT","country":"United States"},"categories":[{"id":"4bf58dd8d48988d198941735","name":"College Academic Building","pluralName":"College Academic Buildings","shortName":"Academic Building","icon":"https:\/\/ss1.4sqi.net\/img\/categories\/education\/default.png","parents":["College & University"],"primary":true}],"verified":false,"stats":{"checkinsCount":1695,"usersCount":281,"tipCount":1},"likes":{"count":0,"groups":[]},"beenHere":{"count":0}}}"

2 个答案:

答案 0 :(得分:2)

在字符串周围使用单引号而不是双引号:

JSON.parse('{"id":"52e2ac6511d28d5cd63ee41f","createdAt":1390586981,"type":"checkin","shout":"Vd","timeZone":"America\/Denver","timeZoneOffset":-420,"user":{"id":"76097213","firstName":"SpencerTesting","lastName":"GardnerTesting","gender":"none","relationship":"self","photo":"https:\/\/foursquare.com\/img\/blank_boy.png","tips":{"count":0},"lists":{"groups":[{"type":"created","count":1,"items":[]}]},"homeCity":"Pleasant Grove, UT","bio":"","contact":{"email":"email@email.com"}},"venue":{"id":"4b83ec97f964a520d81631e3","name":"Joseph Smith Building","contact":{},"location":{"address":"770 E University Pkwy","crossStreet":"at BYU","lat":40.245901424248,"lng":-111.65174603462219,"postalCode":"84604","cc":"US","city":"Provo","state":"UT","country":"United States"},"categories":[{"id":"4bf58dd8d48988d198941735","name":"College Academic Building","pluralName":"College Academic Buildings","shortName":"Academic Building","icon":"https:\/\/ss1.4sqi.net\/img\/categories\/education\/default.png","parents":["College & University"],"primary":true}],"verified":false,"stats":{"checkinsCount":1695,"usersCount":281,"tipCount":1},"likes":{"count":0,"groups":[]},"beenHere":{"count":0}}}')

适合我。

如果字符串是动态生成的并且实际上包含"字符,请遵循dandavis建议使用"删除JSON.parse(str.slice(1,-1)),其中str包含JSON响应到您的Foursquare API调用。

但是,这不应该是必要的,因为周围的引号是打印到控制台的副作用。实际上,checkin键的值是完全有效的JSON,可以使用JSON.parse定期解析。但是,如果从控制台复制,则需要替换额外的",因为所有内部"都不会在控制台中显示为转义。

如果你使用的是jQuery而不是vanilla JS来发送你的AJAX查询,在查询期间指定dataType: 'json'会自动导致传递给success的解析数据打回来。如果响应类型为$.ajax()或等效,我认为这是application/json的默认行为。

答案 1 :(得分:1)

  

我似乎无法将此字符串解析为Javascript中的有效JSON

您不会将字符串解析为 JSON。字符串包含 JSON,您解析它以转换JavaScript数组和对象中的JSON。

在您的情况下,您正在寻找

 var data = JSON.parse(obj.checkin);

因为checking属性的值是包含JSON的字符串。然后你可以access the properties of the object