简体   繁体   中英

\ getting appended in the JSON response. How should i remove this

{
  "content": "{\"text\":\"Executing NodeDatasetFileOrDirectoryCSV : 1\",\"id\":1,\"name\":\"CSV\",\"type\":\"text\"}"
}

\\ tag is getting appended after everything.

I want to access the type field. But i am not able to even after content.type because of the \\ appended after every element. How to remove this ?

You're response is coming down as a valid JSON object, but the content property holds a value that is a JSON string , not a JSON object. You can either fix it on your server-side however you are constructing your response, or you can use JSON.parse to parse the content JSON string into a full-fledged object in JavaScript after you get your response.

The latter would be something like this:

var response = {"content": "{\"text\":\"Executing NodeDatasetFileOrDirectoryCSV : 1\",\"id\":1,\"name\":\"CSV\",\"type\":\"text\"}" };
response.content = JSON.parse(response.content);
console.log(response.content.type);

使用JSON.parse()从字符串获取JSON对象,然后使用键获取值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM