A double quote even if escaped is throwing parse error.
look at the code below

//parse the json in javascript  
var testJson = '{"result": ["lunch", "\"Show\""] }';  
var tags = JSON.parse(testJson);  
alert (tags.result[1]);

This is throwing parse error because of the double quotes (which are already escaped).
Even eval() won't work here.
But if i escape it with double slashes like this:

var result = '{"result": ["lunch", "\\"Show\\""] }';  
var tags = JSON.parse(result);  
alert (tags.result[1]);

then it works fine.
Why do we need to use double slash here in javascript? The problem is that PHP json_encode() function escapes a double quote with a single slash (like this: \"show\") which JSON.parse won't be able to parse. How do i handle this situation?

推荐答案

Well, finally, JSON's parse uses the same eval, so there's no difference when you give them smth. with incorrect syntax. In this case you have to escape correctly your quotes in php, and then escape them and their escaping slashes with json_encode

<?php
    $json = '{"result": ["lunch", "\"Show\""] }';
    echo json_encode($json);
?>

OUTPUT: "{\"result\": [\"lunch\", \"\\\"Show\\\"\"] }"

这应该适用于客户端JS(如果我没有输入错误的话).

Json相关问答推荐

在Oracle中循环整个json_opp_t以更改特定键的值

在Go中,当字段可以根据其他字段具有不同的类型时,什么是正确的方法来卸载JSON?

在PowerShell中,如何获取数据对所在的JSON对象的名称

在Ruby的json中压缩单个字段

将部分数据字节解组到自定义 struct 中

如果主对象中已存在属性,则不应在Jolt中引用次对象

如何获取 JSON 对象字段值和同一 JSON 对象的下一个数组中的字段值?

具有 (RegEx) 模式的 json-schema 中的枚举

如何使用nifi从json文件中过滤属性

将 JSON 解组为具有唯一元素的 map 切片

如何在linux中用jq过滤json数组?

Servicestack 返回数组而不是带有数组的对象

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

如何使用 jq 将 JSON 对象流转换为数组

apple-app-site-association json 文件是否会在应用程序中更新?

有必要清理 JSON 吗?

JSON 到 JSON 转换器

如何将有向无环图 (DAG) 存储为 JSON?

使用 Retrofit 解析动态密钥 Json 字符串

如果键可能不存在,则从 Python dict 读取