Shanmu_92
Shanmu_92

Reputation: 855

Parse JSON throws error while using Special characters

I have parsed the below string but it s not parsed because of the character ('****'),

JSON.parse("{\"data\":\"value \"}")

It throws error,

Uncaught SyntaxError: Unexpected token in JSON at position 15

How can i get rid of this.

Note: In my case i need to skip all the special characters, which came dynamically. So i need to generic solution. Can i make it?

enter image description here

Upvotes: 0

Views: 2135

Answers (1)

Matt
Matt

Reputation: 1973

It seems some special characters are coming from excel file. Try this,

<script type='text/javascript'>
        window.onload = function () {
            var str ='{\"data\":\"value \ \"}'.replace(/\\n/g, "\\n")
               .replace(/\\'/g, "\\'")
               .replace(/\\"/g, '\\"')
               .replace(/\\&/g, "\\&")
               .replace(/\\r/g, "\\r")
               .replace(/\\t/g, "\\t")
               .replace(/\\b/g, "\\b")
               .replace(/\\f/g, "\\f");
            var d = JSON.parse(str);
            alert(d.data);
        }
    </script>

Upvotes: 1

Related Questions