views:

52

answers:

1

I am trying to call JSON.parse, where one of the values is html. I have tried running HtmlEncode, to no avail. FF seems to work fine, however IE7 throws an error.

Here is an example of my json string that I wish to parse.

[{"Title":"Bed","ImageUrl":"~\\/Test\\/sample-product.ashx?w=410","ProductUrl":"\\/products\\/AB10005.aspx","Description":"\r\n\t\t<p>New Bed</p>\r\n","Colours":"Brown, Black, Blue","ProductCode":"AB10005","RRP":"50.000000"},{"Title":"Sample01","ImageUrl":"~\\/Test\\/sample-product.ashx?w=410","ProductUrl":"\\/products\\/Sample01.aspx","Description":"\r\n\t\t<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,%XA0 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr,%XA0 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr,%XA0 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>\r\n","Colours":"Black, Green, Blue","ProductCode":"Sample01","RRP":""}]

EDIT: I am using json2

EDIT: Javascript code:

var fItems = JSON.parse(result);

result is a json string, which is as above. I have replaced all the \r's \n's \t's.

+1  A: 

The \r, \n, \t was causing issues. Once I replaced those characters, everything seemed to work ok.

        j = j.replace(new RegExp("\n","g"),"");
        j = j.replace(new RegExp("\t","g"),"");
        j = j.replace(new RegExp("\r","g"),"");
mickyjtwin
You could combine that into one regular expression, such as:j = j.replace(new RegExp("[\n|\t|\r]","g"),"");
Jason Berry
That regex could be made much shorter - `j = j.replace(/[\n\t\r]/g, '');`
kangax