admin管理员组文章数量:1290861
In my application I am getting a response like below:
{"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."}
How can I convert this into JSON? I tried JSON.parse, but it doesn’t seem to work. Is there another way to convert this string into a valid JSON format?
In my application I am getting a response like below:
{"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."}
How can I convert this into JSON? I tried JSON.parse, but it doesn’t seem to work. Is there another way to convert this string into a valid JSON format?
Share Improve this question edited Feb 1, 2024 at 10:56 yasarui asked Sep 30, 2020 at 20:32 yasaruiyasarui 6,5538 gold badges45 silver badges80 bronze badges 4 |2 Answers
Reset to default 24I understand where the confusion is coming from. The provided object has a property which contains a JSON string. In this case, the "data" attribute contains the JSON string which you need to parse. Look at the following example.
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
Try this:
let data = {"success":true,"data":"{\"status\": \"Failed\", \"percentage_completed\": \"0\", \"actions\": \"Error: Insufficient argument: 1\\n\"}","message":"Logs fetched successfully."}
data.data = JSON.parse(data.data);
console.log(data);
本文标签: How to convert a string to JSON in JavaScriptStack Overflow
版权声明:本文标题:How to convert a string to JSON in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738422764a2085949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
JSON.parse()
didn't work? Did you get an error message? – zcoop98 Commented Sep 30, 2020 at 20:36JSON.parse(response.data)
. – Dipen Shah Commented Sep 30, 2020 at 20:37