开发者

Why is the second JSON object invalid?

开发者 https://www.devze.com 2023-03-27 01:39 出处:网络
The first result parses $.parseJSON(data) just fine: {\"result\":{\"output\":\"<table cellspacing=\\\"0\\\" cellpadding=\\\"0\\\" border=\\\"0\\\" id=\\\"krd_table\\\" class=\\\"0\\\"><tr>

The first result parses $.parseJSON(data) just fine:

{"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>Vi开发者_如何学Pythonew</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"}}

The second however, through the same output and construction results in invalid 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"}}

Any ideas why this is happening? Thanks Rich

UPDATE: removed slashes, but it still gets invalidated. Here's is the version without. JSONLint tells me:

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

Follow Up I figured out the double quotes around the attributes were causing the problems here and needed to be escaped. As everyone pointed out single quotes within the content, should not be escaped and also keys & values need to be surrounded within double quotes. Thanks for everyone's input.


You don't need to escape single quotes. Replace \' with ' and you will validate.


Children\'s Community Development Center

Don't escape single quotes in a double quoted string. You have instances of \' of that in your string. Remove the needless \ and it validates fine.


You are escaping your single apostrophes, don't do that.


I think the issue here is the escaped single quotes in Kim\'s Agency and Children\'s Community Development Center - these don't need to be escaped, only the double quotes.

0

精彩评论

暂无评论...
验证码 换一张
取 消