如何在Javascript中将字符串转换为json

cod*_*eek -2 javascript json

在我的应用程序中,我收到如下回复

{"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."}
Run Code Online (Sandbox Code Playgroud)

如何将其转换为 JSON。试过 JSON.parse 似乎不起作用。有没有其他方法可以将此字符串转换为有效的 JSON 格式

GLJ*_*GLJ 9

我明白混乱的来源。提供的对象有一个包含 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
Run Code Online (Sandbox Code Playgroud)