admin管理员组文章数量:1389903
I have a JSON response that is not properly formatted and has speical characters ing in and I want to clean it up by removing the special characters using string.replace(). For some reason it is not working.
Here is the JSON result
[{"User::newPassword":["Password could not be changed. The request can't be validated"]},[]]
and here is my expression.
resp.replace(::g, '');
But it doesn't seem to work. Any advice on this would be appreciated as there is nothing I can do for it on the backend.
I have a JSON response that is not properly formatted and has speical characters ing in and I want to clean it up by removing the special characters using string.replace(). For some reason it is not working.
Here is the JSON result
[{"User::newPassword":["Password could not be changed. The request can't be validated"]},[]]
and here is my expression.
resp.replace(::g, '');
But it doesn't seem to work. Any advice on this would be appreciated as there is nothing I can do for it on the backend.
Share Improve this question edited Aug 31, 2015 at 4:18 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Aug 31, 2015 at 3:31 Bazinga777Bazinga777 5,30113 gold badges56 silver badges95 bronze badges1 Answer
Reset to default 5You cannot use replace()
on JSON.
If you're working with strings
::
should be in quotes if you want to replace first occurrence of it.
resp.replace('::', '');
If you want to replace all occurrences, use /
as delimiter of regex.
resp.replace(/::/g, '');
If you're working with JSON
- Convert the
JSON
to string usingJSON.stringify()
- Use
replace
on string - Convert
string
back toJSON
usingJSON.parse()
Using Object methods
You can also change the key
to remove ::
from it
var newKey = key.replace('::', ''); // Create new key by replacing the `::`
obj[newKey] = obj[key]; // Add new key and value in object
delete key; // Remove old key
本文标签: javascriptString replace from JSONStack Overflow
版权声明:本文标题:javascript - String replace from JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744584010a2614089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论