JSON

2014-01-30 1 views
2

내가 누구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에 의해 시도 JSON 문자열 다음 날 gauide 또는 나에게 해석의 적절한 방법을 제안 할 수 있습니다 JSON에 다음 문자열을 구문 분석하려고 할 때 문제가있는에 문자열을 구문 분석하는 방법 메서드가 발생하지만 오류가 발생합니다.

Uncaught SyntaxError: Unexpected token 
+0

그것은 배열입니다. 이것이 JSON.parse가 작동하지 않는 이유입니다. – Danny

+0

@ 대니 - JSON은 최상위 수준에서 배열을 허용합니다. – Quentin

+0

@ Quentin 아, 결코 그것을 알지 못했습니다. 그것을 알지 못했습니다. – Danny

답변

4

JSON 문자열 내에 제어 문자를 사용할 수 없습니다. 문자

자바 스크립트 문자열이 포함

"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" 

문자 적 ​​새로운 라인이 금지된다. 당신이 데이터를 원하는 경우에, 당신은 자바 스크립트의 이스케이프 문자를 이스케이프해야합니다 :

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

은, 그러나, JSON을 가정하는 것은 신뢰할 수있는 소스에서, 당신은 안 JSON의 문자열 리터럴을 JavaScript에 삽입해야합니다. '을 처음부터 끝내면 JS 내부에서 파싱 할 필요가없는 JavaScript 객체 리터럴을 갖게됩니다.

0

\r\n이있는 데이터 자체에 문제가 있습니다. json 문자열에 contorl chars를 사용할 수 없기 때문입니다. 당신이 그들을 제거하는 경우

하면 모든 일이 제대로 문제는 JSON에서 이스케이프 문자에서 발생되는

http://jsfiddle.net/DwXnb/

+0

제어 문자는 유효하지 않을 수 있지만 이스케이프 시퀀스는 유효합니다. 그것들은 제거 될 필요가 없습니다. 단지'''와'''문자열 모두를 위해 이스케이프 될 필요가 있습니다. \\ r \\ n' –

1

아래의 바이올린을 참조하십시오. 예 : http:\/\/\n. 당신이 그 (http:\\/\\/\\n)을 두 번 탈출하면

, 구문 분석 작동합니다 :

Demo