admin管理员组文章数量:1332865
I use JSON.stringify
and get the following result
{
"key1": "1",
"key2": "2",
"key3": [
{
"jobCode": "full",
"ine": "1",
"desc": "test"
},
{
"jobCode": "xxx",
"ine": "2",
"desc": "test"
}
]
}
But I need to get the following result
{
"key1": "1",
"key2": "2",
"key3": [
{
\"jobCode\": \"full\",
\"ine\": \"1\",
\"desc\": \"test\"
},
{
\"jobCode\": \"xxx\",
\"ine\": \"2\",
\"desc\": \"test\"
}
]
}
I want to make sure that value for key3 gets in quotes. and internal value quotes get escaped
I tried JSON.strigify
but it gives me the first result but i need the second
I use JSON.stringify
and get the following result
{
"key1": "1",
"key2": "2",
"key3": [
{
"jobCode": "full",
"ine": "1",
"desc": "test"
},
{
"jobCode": "xxx",
"ine": "2",
"desc": "test"
}
]
}
But I need to get the following result
{
"key1": "1",
"key2": "2",
"key3": [
{
\"jobCode\": \"full\",
\"ine\": \"1\",
\"desc\": \"test\"
},
{
\"jobCode\": \"xxx\",
\"ine\": \"2\",
\"desc\": \"test\"
}
]
}
I want to make sure that value for key3 gets in quotes. and internal value quotes get escaped
I tried JSON.strigify
but it gives me the first result but i need the second
-
1
Your desired result is invalid JSON though - it couldn't be
JSON.parse
d correctly, this sounds like a very odd thing to want to do – CertainPerformance Commented Dec 9, 2018 at 5:02 - the reason i need it because spring boot java bean accepts the second one – john Commented Dec 9, 2018 at 5:04
-
spring boot accepts a
String
and then it does the rest. – john Commented Dec 9, 2018 at 5:06
1 Answer
Reset to default 5If you use JSON.stringify on key3, then replace it into the object, it will be passed as a string.
var obj = {"key1":"1","key2":"2","key3":[{"jobCode":"full","ine":"1","desc":"test"},{"jobCode":"xxx","ine":"2","desc":"test"}]};
var jsonKey3 = JSON.stringify(obj.key3);
obj.key3 = jsonKey3;
console.log(obj);
console.log(JSON.stringify(obj));
本文标签: javascriptHow to correctly escape quotes in JSONstringifyStack Overflow
版权声明:本文标题:javascript - How to correctly escape quotes in JSON.stringify? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292851a2448136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论