user881703
user881703

Reputation: 1199

Json String can't decode when end of string have `\'

When JSON string has \ at the end of any string it gives me:

Ext.JSON.decode(): You're trying to decode an invalid JSON String

JSON decode error:

Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String:

[{"ID1":"1","ID2":"1","NAME":"Act\"},{"ID1":"3","ID2":"1","NAME":"Act\"},{"ID1":"4","ID2":"2","NAME":"act $#%^&&*"},{"ID1":"2","ID2":"2","NAME":"act $#%^&&*"}]

How can i avoid above error?

Upvotes: 0

Views: 920

Answers (3)

romuleald
romuleald

Reputation: 1434

If you test your JSON, you will see that it's not valid : http://jsonlint.com/ You'll need to escape your \ with another \

\\Act

You should encode your JSON before, or make it manually if possible.

Upvotes: 0

Andy
Andy

Reputation: 63524

You need to escape that backslash. Initially I thought you would only need to use one escape character, but, according to this demonstration, you need to use three:

{"ID1":"1","ID2":"1","NAME":"Act\\\\"}

Upvotes: 0

doldt
doldt

Reputation: 4506

That is invalid JSON, because the \ character escapes the " mark which would be responsible for closing the string - as such, your string remains unclosed (that is, until the next " comes around).

So your problem is that a backslash has a special meaning inside strings. If you want to use slashes, use \\ instead. (Note that what this does is escape the backslash character itself.)

ie.:

{"ID1":"1","ID2":"1","NAME":"Act\\"}

Upvotes: 2

Related Questions