我想使用eval()
或JSON.parse()
在javascript对象数组中评估字符串,但字符串中的特殊字符\\'
会出现问题。
我已经尝试使用正则表达式,然后尝试解析像
stringData = stringData.replace(/\\'/g, "\'");
console.log(eval(stringData ));
但它不起作用。
我的stringData
变量包含以下值
'[{"club_id":"30","club_name":"Al Lawrence","club_code":"ALRC","club_desc":"We welcome anyone who enjoys the camaraderie of competition at any level. Al currently coaches many Houston area runners ranging from elite to back-of-the-packers. Today the club still has a number of members who receive professional independent coaching from Al, but that certainly is not a requirement for membership in the Al Lawrence Running Club.","club_latitude":"29.770042","club_longitude":"-95.37","club_zip":"77001","club_member_size":"93","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.alrcus.org\/","club_img":"1390975493.jpg","is_published":"1"},{"club_id":"31","club_name":"Bay Area Fit","club_code":"BAF","club_desc":"Welcome to Houston FIT! We are a group of training partners and friends preparing together for a full or half marathon in 2013\/2014. Even if you\u00e2\u20ac\u2122ve never run a step before, training with our group will help you conquer mental and physical barriers you never thought possible.","club_latitude":"29.564405","club_longitude":"-95.091113","club_zip":"77058","club_member_size":"302","club_size":"l","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":"1390975687.jpg","is_published":"1"},{"club_id":"32","club_name":"Bay Area Running Clubub","club_code":"BARC","club_desc":"Bay Area Running Clubub fgfhjhg ghghgj","club_latitude":"","club_longitude":"","club_zip":"77586","club_member_size":"50","club_size":"s","club_type":"non-training","club_url":"http:\/\/www.barchouston.com\/","club_img":"1391165362.","is_published":"1"},{"club_id":"33","club_name":"Bayou City Road Runners","club_code":"BCRR","club_desc":"The 1st race of the HARRA 2014 Spring Series\r\nSaturday, February 22nd, 2014 Houston, Texas\r\nSponsored by the Houston Harriers","club_latitude":"29.73304211","club_longitude":"-95.33655524","club_zip":"77001","club_member_size":"350","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.bcrr.org\/","club_img":"1391078677.jpg","is_published":"1"},{"club_id":"34","club_name":"Brian O\\'Neill\\'s","club_code":"BONRC","club_desc":"Brian O\\'Neill\\'s Brian O\\'Neill\\'s\r\nBrian O\\'Neill\\'s\r\n\r\n","club_latitude":"","club_longitude":"","club_zip":"77005","club_member_size":"360","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.bonrunningclub.com\/","club_img":"1391179316.","is_published":"1"},{"club_id":"35","club_name":"Champions Fit","club_code":"CHF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77070","club_member_size":"230","club_size":"m","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":" ","is_published":"1"},{"club_id":"36","club_name":"CityCentre Running Club","club_code":"CCRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77024","club_member_size":"320","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.ccrunningclub.com\/","club_img":" ","is_published":"1"},{"club_id":"37","club_name":"College Station Running Club","club_code":"CSRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77840","club_member_size":"89","club_size":"s","club_type":"training","club_url":"http:\/\/collegestationrunning.com\/","club_img":" ","is_published":"1"},{"club_id":"38","club_name":"Cypress Fit","club_code":"CYF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77429","club_member_size":"423","club_size":"l","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":" ","is_published":"1"},{"club_id":"39","club_name":"Cypress Running Club","club_code":"CRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77410","club_member_size":"360","club_size":"l","club_type":"training","club_url":"http:\/\/cypressrunningclub.com\/","club_img":" ","is_published":"1"},{"club_id":"40","club_name":"Finish Line Sports Running Club","club_code":"FLS","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77478","club_member_size":"200","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.slfinishlinesports.com\/home.html","club_img":" ","is_published":"1"},{"club_id":"41","club_name":"First Colony Community Asscociation","club_code":"FCCA","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77479","club_member_size":"200","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.firstcolony.org\/","club_img":" ","is_published":"1"},{"club_id":"42","club_name":"Fort Bend Fit","club_code":"FBF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77459","club_member_size":"356","club_size":"l","club_type":"training","club_url":"http:\/\/www.fortbendfit.org\/","club_img":" ","is_published":"1"},{"club_id":"43","club_name":"Galloway Houston","club_code":"GALL","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"340","club_size":"l","club_type":"training","club_url":"https:\/\/www.facebook.com\/pages\/Galloway-Houston-Running-Group\/104100839668839","club_img":" ","is_published":"1"},{"club_id":"44","club_name":"Galveston Fit","club_code":"GF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77550","club_member_size":"430","club_size":"l","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":" ","is_published":"1"},{"club_id":"45","club_name":"Gooses Acres Running Club","club_code":"GARC","club_desc":"Gooses Acres Running Club Gooses Acres Running ClubGooses Acres Running Club\r\nGooses Acres Running Club\r\nGooses Acres Running Club\r\nGooses Acres Running Club\r\nGooses Acres Running Club\r\nGooses Acres Running Club\r\nGooses Acres Running Club\r\n\r\n","club_latitude":"","club_longitude":"","club_zip":"77380","club_member_size":"400","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.goosesacrerunningclub.com\/","club_img":"1391172832.","is_published":"1"},{"club_id":"46","club_name":"Gulf Coast Running Club","club_code":"GCRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77573","club_member_size":"121","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.txgcrc.org\/","club_img":"1391160146.","is_published":"1"},{"club_id":"47","club_name":"Houston Harriors","club_code":"HH","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"140","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.houstonharriers.com\/","club_img":" ","is_published":"1"},{"club_id":"48","club_name":"Houston Fit","club_code":"HF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"490","club_size":"","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":"1391160352.jpg","is_published":"1"},{"club_id":"49","club_name":"Houston Master Sports Asscocation","club_code":"HMSA","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"150","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.houstonmasters.org\/","club_img":" ","is_published":"1"},{"club_id":"50","club_name":"H-Town Runners","club_code":"HTR","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"78","club_size":"s","club_type":"non-training","club_url":"http:\/\/www.h-townrunners.com\/","club_img":" ","is_published":"1"},{"club_id":"51","club_name":"In Flight Running","club_code":"IFR","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"120","club_size":"m","club_type":"training","club_url":"http:\/\/www.inflightrunning.com\/","club_img":" ","is_published":"1"},{"club_id":"52","club_name":"Jackies Brickhouse Running Club","club_code":"JBRC","club_desc":"Jackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\nJackies Brickhouse Running Club\r\n","club_latitude":"","club_longitude":"","club_zip":"77565","club_member_size":"423","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.ffprunningclubs.org\/","club_img":"1391172903.","is_published":"1"},{"club_id":"53","club_name":"Katy Fit","club_code":"KATYF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77494","club_member_size":"550","club_size":"l","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":" ","is_published":"1"},{"club_id":"54","club_name":"Kenyan Way","club_code":"KENW","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"460","club_size":"l","club_type":"training","club_url":"http:\/\/www.kenyanway.com\/","club_img":" ","is_published":"1"},{"club_id":"55","club_name":"Kingwood Fit","club_code":"KINGF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77339","club_member_size":"400","club_size":"l","club_type":"training","club_url":"http:\/\/www.houstonfit.com\/","club_img":" ","is_published":"1"},{"club_id":"56","club_name":"Kung Fu Running Club","club_code":"KFRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77007","club_member_size":"436","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.ffprunningclubs.org\/","club_img":" ","is_published":"1"},{"club_id":"57","club_name":"ODM Steppers","club_code":"ODMS","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77011","club_member_size":"90","club_size":"s","club_type":"non-training","club_url":"http:\/\/www.opendoorhouston.org\/","club_img":" ","is_published":"1"},{"club_id":"58","club_name":"Pearland Area Runners Club","club_code":"PARC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77584","club_member_size":"50","club_size":"s","club_type":"non-training","club_url":"http:\/\/www.pearlandarearunnersclub.org\/","club_img":" ","is_published":"1"},{"club_id":"59","club_name":"PTI America","club_code":"PIM","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"160","club_size":"m","club_type":"non-training","club_url":"no website","club_img":" ","is_published":"1"},{"club_id":"60","club_name":"Runners High","club_code":"RH","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"200","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.runnershighclub.com\/","club_img":" ","is_published":"1"},{"club_id":"61","club_name":"Seven Hills","club_code":"SH","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"130","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.7hills.us\/","club_img":" ","is_published":"1"},{"club_id":"62","club_name":"Striders","club_code":"STS","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"400","club_size":"l","club_type":"non-training","club_url":"http:\/\/thehoustonstriders.org\/","club_img":" ","is_published":"1"},{"club_id":"63","club_name":"Terlingua Track Club","club_code":"TTC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"120","club_size":"m","club_type":"non-training","club_url":"http:\/\/terlinguatrackclub.org\/","club_img":" ","is_published":"1"},{"club_id":"64","club_name":"Texas Running Club","club_code":"TRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77478","club_member_size":"90","club_size":"s","club_type":"non-training","club_url":"no website","club_img":" ","is_published":"1"},{"club_id":"65","club_name":"The Woodlands Running Club","club_code":"TWRC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77393","club_member_size":"465","club_size":"l","club_type":"non-training","club_url":"http:\/\/thewoodlandsrunningclub.org\/","club_img":" ","is_published":"1"},{"club_id":"66","club_name":"Tornados","club_code":"TOR","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77001","club_member_size":"79","club_size":"m","club_type":"non-training","club_url":"http:\/\/www.tornadosrunningclub.com\/","club_img":" ","is_published":"1"},{"club_id":"67","club_name":"West End Running Club","club_code":"WERC","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77056","club_member_size":"480","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.ffprunningclubs.org\/","club_img":" ","is_published":"1"},{"club_id":"68","club_name":"Woodlands Fit","club_code":"WF","club_desc":"","club_latitude":"","club_longitude":"","club_zip":"77393","club_member_size":"366","club_size":"l","club_type":"training","club_url":"http:\/\/www.woodlandsfit.com\/","club_img":" ","is_published":"1"}]'
如果有人知道解决方案,请指导我。
答案 0 :(得分:0)
这是有效的JSON - 只是从它的任一侧剥离单引号并验证它。使用JSONLint之类的工具对其进行测试。那就是如果你的单引号只是字符串包装器 - 你应该没有问题。
如果您从远程数据源中提取此JSON,则无论如何都不会手动定义字符串,因此它不应该是一个问题 - 数据源应该在适用的情况下正确地转义它自己的引号。 (这个逻辑内置于解析器中。
但是,如果您实际上(不确定原因)想要将其作为字符串写入,并且您使用单引号作为字符串标识符,请使用'
{{{} {{} {{}} { {1}}。
示例:
\
点击Fiddle
你的\'
函数可能正在破坏,那就是你正在尝试对字符串执行替换,将其替换为文字单引号(var str = '[{"club_id":1, "name":"Josh O\'Hare"}, {"club_id":12, "name":"Bob O\'Dale"}, {"club_id":16, "name":"Amy O\'Malley"}, {"club_id":1, "name":"Albert O\'Connell"}]';
是单引号,而不是转义字符后跟单引号。)