如何将字符串解析为json

时间:2014-01-30 13:26:32

标签: 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

3 个答案:

答案 0 :(得分:4)

您不能在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对象文字。

答案 1 :(得分:1)

问题是由JSON中的转义字符引起的。例如:http:\/\/\n

如果你对它们进行双重转义(http:\\/\\/\\n),则解析将起作用:

Demo

答案 2 :(得分:0)

你的问题出现在你拥有\r\n的数据本身,因为你不能在json字符串中使用contorl chars。

如果你删除它们一切正常,请看下面的小提琴

http://jsfiddle.net/DwXnb/