为什么第二个JSON对象无效?

时间:2011-08-08 18:19:50

标签: javascript json

第一个结果解析$ .parseJSON(数据)就好了:

{"result":{"output":"<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" id=\"krd_table\" class=\"0\"><tr><th>Agency Name</th><th>Email</th><th>Phone</th><th>Edit</th><th>View</th></tr><tr><td>CLASP Homes - Westport</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/31\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/31\" class=\"view_row\">View</a></td></tr><tr><td>Horizons at Greens Farms</td><td class=\"item\">me@email.org OR mme@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/67\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/67\" class=\"view_row\">View</a></td></tr></table>", "num_results":"2"}}

然而,第二个,通过相同的输出和构造导致无效的JSON:

{"result":{"output":"<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" id=\"krd_table\" class=\"0\"><tr><th>Agency Name</th><th>Email</th><th>Phone</th><th>Edit</th><th>View</th></tr><tr><td>Center for Prevention & Recovery - Positive Directions (The)</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-22224</td><td class=\"item\"><a href=\"/agencies/edit/23\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/23\" class=\"view_row\">View</a></td></tr><tr><td>Children\'s Community Development Center</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/28\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/28\" class=\"view_row\">View</a></td></tr><tr><td>CLASP Homes - Westport</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/31\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/31\" class=\"view_row\">View</a></td></tr><tr><td>Horizons at Greens Farms - Westport</td><td class=\"item\">me@email.org OR me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/67\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/67\" class=\"view_row\">View</a></td></tr><tr><td>Interfaith Housing Association</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/72\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/72\" class=\"view_row\">View</a></td></tr><tr><td>Project Return</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/103\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/103\" class=\"view_row\">View</a></td></tr><tr><td>ITNCoastalCT (formerly CoastalCT Senior Transportation)</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/164\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/164\" class=\"view_row\">View</a></td></tr><tr><td>Kim\'s Agency</td><td class=\"item\">me@email.org</td><td class=\"item\">222-222-2222</td><td class=\"item\"><a href=\"/agencies/edit/189\" class=\"edit_row\">Edit</a></td><td class=\"item\"><a href=\"/agencies/view/189\" class=\"view_row\">View</a></td></tr></table>", "num_results":"8"}}

为什么会发生这种情况? 谢谢 富

更新: 删除了斜杠,但它仍然无效。这是没有的版本。 JSONLint告诉我:

Parse error on line 3:
...{        "output": '<tablecellspacing="
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['




{"result":{"output":'<table cellspacing="0" cellpadding="0" border="0" id="krd_table" class="0"><tr><th>Agency Name</th><th>Email</th><th>Phone</th><th>Edit</th><th>View</th></tr><tr><td>CLASP Homes - Westport</td><td class="item">me@email.com</td><td class="item">222-222-2222</td><td class="item"><a href="/agencies/edit/31" class="edit_row">Edit</a></td><td class="item"><a href="/agencies/view/31" class="view_row">View</a></td></tr><tr><td>Horizons at Greens Farms - Westport</td><td class="item">me@email.com</td><td class="item">222-222-2222</td><td class="item"><a href="/agencies/edit/67" class="edit_row">Edit</a></td><td class="item"><a href="/agencies/view/67" class="view_row">View</a></td></tr></table>', "num_results":"2"}}

跟进 我发现属性周围的双引号导致了这里的问题,需要进行转义。正如每个人都指出内容中的单引号,不应该被转义,而且键和&amp;值必须用双引号括起来。 感谢大家的投入。

4 个答案:

答案 0 :(得分:3)

您无需转义单引号。将\'替换为',您将验证。

答案 1 :(得分:3)

Children\'s Community Development Center

不要在双引号字符串中转义单引号。您的字符串中有\'个实例。删除不必要的\,它验证正常。

答案 2 :(得分:1)

你正在逃避你的单撇号,不要这样做。

答案 3 :(得分:1)

我认为这里的问题是Kim\'s AgencyChildren\'s Community Development Center中的转义单引号 - 这些引号不需要转义,只需要双引号。