最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse map by JSON.stringify - Stack Overflow

programmeradmin5浏览0评论

I have some object with map data and I need to convert it to JSON. I am using JSON.stringify() method, but after conversion I am getting next result:

{"someval":1,"someval2":"blabla","my_map":["{\"email\":\"[email protected]\",\"fullName\":\"username\"}"],"moredata":""}

I need to get clear string for my_map, without extra '\' characters. How I can remove them? Just replace doesn't works because other values can have such characters.

I have some object with map data and I need to convert it to JSON. I am using JSON.stringify() method, but after conversion I am getting next result:

{"someval":1,"someval2":"blabla","my_map":["{\"email\":\"[email protected]\",\"fullName\":\"username\"}"],"moredata":""}

I need to get clear string for my_map, without extra '\' characters. How I can remove them? Just replace doesn't works because other values can have such characters.

Share Improve this question asked Oct 14, 2017 at 13:03 TedTed 1,7604 gold badges31 silver badges55 bronze badges 6
  • You can use replace(regex) – Sirwan Afifi Commented Oct 14, 2017 at 13:10
  • 2 @SirwanAfifi, that would break the JSON. – Mouser Commented Oct 14, 2017 at 13:13
  • 1 No, do NOT use any sort of regex or replaced method. JSON processing should be done only by JSON methods – JAAulde Commented Oct 14, 2017 at 13:13
  • 3 @SirwanAfifi that is a terrible suggestion – charlietfl Commented Oct 14, 2017 at 13:13
  • 1 Why does it have to be a string? It’s very unusual to have a JSON string inside JSON. There isn’t really any benefit to do that. my_map should simply be an array containing an object, not a string. But if you really want it to be a string then the inner have to be escaped. There is no way around it. – Felix Kling Commented Oct 14, 2017 at 13:35
 |  Show 1 more ment

2 Answers 2

Reset to default 2

I'm assuming my_map is already JSON. To be exact I need to see the original map object, however the example below will provide enough insight to solve your problem with.

//correct example of a JavaScript object
var map = {"someval":1,"someval2":"blabla","my_map":[{"email":"[email protected]","fullName":"username"}],"moredata":""};

//also correct but the array content in my_map is a string and not treated as an object.
var map2 = {"someval":1,"someval2":"blabla","my_map":["{\"email\":\"[email protected]\",\"fullName\":\"username\"}"],"moredata":""};

console.log(JSON.stringify(map));   //correctly parsed
console.log(JSON.stringify(map2));  //incorrectly parsed

//solution for two:
//convert the string (JSON) to array and stringify result
map2["my_map"] = JSON.parse(map2["my_map"]);
console.log(JSON.stringify(map2));

You can use JSON.parse()

In this case my_map contains an array which contains a string which needs to be JSON parsed. So to get the string you can do my_map[0]. See example:

var x = {"someval":1,"someval2":"blabla","my_map":["{\"email\":\"[email protected]\",\"fullName\":\"username\"}"],"moredata":""};

var xx = JSON.parse(x.my_map[0]);

console.log('1: ' + xx.email);
console.log('2: ' + xx.fullName);

发布评论

评论列表(0)

  1. 暂无评论