javascript - 如何将字符串解析为json

标签 javascript json

当我尝试将以下字符串解析为 json 时遇到困难,任何人都可以引导我或建议我解析以下 json 字符串的正确方法

'[{"club_id":"19","club_name":"Al Lawrence","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":"s","club_type":"training","club_url":"http:\/\/www.alrcus.org\/","club_img":"1390975493.jpg","is_published":"1"},{"club_id":"20","club_name":"Bay Area Fit","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\u2019ve 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":"22","club_name":"Bay Area Running Club","club_desc":"Order now before the next HARRA race!  Password texas.  Color, size and logo location options available plus you can add BARC on the back for just $5 more.  Flat shipping rate of $4.95 if you buy one or two singlets and no tax!  Orders are shipped out 24-48 hours after purchase. ","club_latitude":"29.583945","club_longitude":"-95.033832","club_zip":"77586","club_member_size":"450","club_size":"l","club_type":"non-training","club_url":"http:\/\/www.barchouston.com\/","club_img":"1390989179.jpg","is_published":"1"},{"club_id":"29","club_name":"Houston Harriors","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.733042110628393","club_longitude":"-95.33655524253845","club_zip":"77001","club_member_size":"0","club_size":"","club_type":"tranning","club_url":"http:\/\/www.houstonharriers.com\/","club_img":"1391078677.jpg","is_published":"1"}]'

我尝试过eval()JSON.parse方法,但出现错误

Uncaught SyntaxError: Unexpected token

最佳答案

JSON 字符串中不能包含控制字符。

您的 JavaScript 字符串文字包括:

"The 1st race of the HARRA 2014 Spring Series\r\nSaturday, February 22nd, 2014 Houston, Texas\r\nSponsored by the Houston Harriers"

这将转换为 JSON 片段:

"The 1st race of the HARRA 2014 Spring Series
Saturday, February 22nd, 2014 Houston, Texas
Sponsored by the Houston Harriers"

禁止直接换行。如果您希望它们出现在数据中,那么您必须在 JavaScript 中转义转义字符:

"The 1st race of the HARRA 2014 Spring Series\\r\\nSaturday, February 22nd, 2014 Houston, Texas\\r\\nSponsored by the Houston Harriers"

但是,一般来说,假设 JSON 来自受信任的来源,那么您首先就不应该在 JavaScript 中嵌入 JSON 字符串文字。只要去掉开头和结尾的 ' ,你就会得到一个不需要在 JS 内部解析的 JavaScript 对象文字。

关于javascript - 如何将字符串解析为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457830/

相关文章:

javascript - Cufon 在 FBML 静态页面中工作吗?

java - Wicket 和 jQuery 重叠/碰撞

Javascript 函数和声明

java - 使用 Streaming API 和 Json Parser 进行 JSON 解析 - 混合模式

具有一个强制性属性和至少另一个未键入的 json 模式检查

javascript function rotate element => 尝试为传入的特定参数设置 IF 条件

javascript - 来自指令的 Angular 范围变量不以 ng 样式同步

javascript - jQuery、ajax 和 jsonp 的问题

javascript - 如果存在 2 个或更多相同的结果,则将 select2 建议限制为 1

javascript - 如何在 Angular 中加载嵌套数组?