Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse json object That stringify Twice

How can i parse some object that stringify twice?

Most of the time in my code it is not problem but some times it stringify twice, and I can not trace the code to find my problem.

My JSON object something like this:

""[{\"name\":\"trane\",\"price\":\"150000\",\"order\":\"\",\"sale\":\"\",\"printedPic\":\"\",\"remainingPic\":\"\",\"locationEncome\":\"\"}]""
like image 480
Pooria.Shariatzadeh Avatar asked Aug 30 '26 00:08

Pooria.Shariatzadeh


1 Answers

It's definitely best to figure out where and why it's stringifying twice, but you can just parse twice if you have to.

JSON.parse(JSON.parse("JSON STRING HERE"))

Edit

Potentially you're stringifying an already stringified object, this could help you figure out what is going wrong.

Add this function to your code and then replace your JSON.stringify calls to JSON.stringifyIfObject calls. Just use this as a means of debugging though, I wouldn't put this into production.

JSON.stringifyIfObject = function stringifyIfObject(obj){
    if(typeof obj == "object")
        return JSON.stringify(obj);
    else{
        alert("found already stringified object")
        return obj;
    }
}
like image 151
tcigrand Avatar answered Aug 31 '26 14:08

tcigrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!