[英]How to convert string to json in Javascript
在我的应用程序中,我收到如下回复
{"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."}
如何将其转换为 JSON。 试过 JSON.parse 似乎不起作用。 有没有其他方法可以将此字符串转换为有效的 JSON 格式
我明白混乱的来源。 提供的对象有一个包含 JSON 字符串的属性。 在这种情况下,“数据”属性包含您需要解析的 JSON 字符串。 看下面的例子。
var result = {"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."};
JSON.parse(result); // should fail
JSON.parse(result["data"]); // should work
JSON.parse(result.data) // or if you prefer this notation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.