从无效转换为有效json [英] convert from invalid to valid json
本文介绍了从无效转换为有效json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有包含非法字符的json
I have json which contains illegal characters
{"message":"A"B",
"fromWhom":"53"}
我想要从服务器弹簧发送到客户端的纯文本,以便客户端可以获取完整的数据.
I want plaint text which is sent from the sever spring to client, so that client can get the complete data.
如何将字符串的非法字符替换为有效的json对象?
How can I replace illegal characters of a string to a valid json object?
推荐答案
我认为您需要转义报价.
I think you need to escape the quote.
{
"message": "A\"B",
"fromWhom": "53"
}
关于更改值.我认为您需要JSON.parse.
As for changing the value. I think you need to JSON.parse.
示例:
<script>
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
obj = JSON.parse(text);
obj.employees[1].lastName = "Timmy" //Value change here
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
这篇关于从无效转换为有效json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文