簡體   English   中英

如何在Javascript中將字符串轉換為json

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM