1

the server returns data:

{
    "items": [{
        "id": "671",
        "post_title": "Seche Vite Dry Fast Top Coat",
        "post_content": "<span style=\"color\: \#ff0000\;\"><strong>Roy Recommends:<\/strong>\u00c2\u00a0 Edith, our in-house nail expert, swears by this product.\u00c2\u00a0 No manicure or pedicure is complete without adding this top coat as the finishing touch.<\/span>\r\n\r\nSeche Vite\u00e2\u201e\u00a2 dry fast top coat is widely acknowledged as the world\'s finest top coat.\u00c2\u00a0 Specially formulated to penetrate through nail lacquer to the base coat forming a single solid coating over the nail plate for a much more durable finish. Guaranteed not to yellow while leaving nails silky, stronger and resistant to chipping and peeling.\r\n\r\n&nbsp;",
        "post_excerpt": "",
        "post_status": "publish",
        "post_parent": "0",
        "parent_sort_id": "671",
        "prod_meta_data_key": "_wpsc_product_metadata",
        "category": "Roy Recommends,Nail Care",
        "_wpsc_price": "10",
        "_wpsc_special_price": "0",
        "_wpsc_sku": "",
        "_wpsc_stock": "4",
        "unpublish_when_none_left": "1",
        "weight": 8.5,
        "weight_unit": "ounce",
        "height": "0",
        "height_unit": "in",
        "width": "0       ",
        "width_unit": "in",
        "length": "0",
        "length_unit": "in",
        "local": "0",
        "international": "0",
        "no_shipping": "0"
    }],
    "totalCount": "1"
}

Iam getting the foll err:

Uncaught SyntaxError: Unexpected token ILLEGAL
doDecodeext-all.js:7
(anonymous function)ext-all.js:7
o.callbacksm.js:249
Ext.extend.handleResponseext-all.js:7
fext-base.js:7
mext-base.js:7
(anonymous function)

I found a online tool: http://json.parser.online.fr/

In which when I paste the data eval is not able to evaluate the space between span & style as eval can execute only expression not statement. here break is a statement:

<span style=\"color\: \#ff0000\;\">

when i do preg_replace("/[ ]+/",'&nbsp;',$data) it works. Now the problem is I don't want   to appear as we allow the user to edit the text & I want to it to appear it as space only.

1
  • 1
    which method u r using to read above json string?? Commented Aug 12, 2011 at 7:57

2 Answers 2

1

Using JSONLint I see that your JSON is invalid. You must escape backslashes (\) when they're not part of legal JSON escape sequences (\", \\, \/, \b, \f, \n, \r, \t, \u<4-digit-hex>).

In this case, the value of post_content is illegal since it has sequences like \: and \#. Changing it to the following should fix the problem (of course, you'll have to fix your server-side code to properly escape the JSON response)

{
    "items": [
        {
            "id": "671",
            "post_title": "Seche Vite Dry Fast Top Coat",
            "post_content": "<span style=\"color\\: \\#ff0000\\;\"><strong>Roy Recommends:</strong>  Edith, our in-house nail expert, swears by this product.  No manicure or pedicure is complete without adding this top coat as the finishing touch.</span>\u000d\u000a\u000d\u000aSeche Vite™ dry fast top coat is widely acknowledged as the world\\'s finest top coat.  Specially formulated to penetrate through nail lacquer to the base coat forming a single solid coating over the nail plate for a much more durable finish. Guaranteed not to yellow while leaving nails silky, stronger and resistant to chipping and peeling.\u000d\u000a\u000d\u000a&nbsp;",
            "post_excerpt": "",
            "post_status": "publish",
            "post_parent": "0",
            "parent_sort_id": "671",
            "prod_meta_data_key": "_wpsc_product_metadata",
            "category": "Roy Recommends,Nail Care",
            "_wpsc_price": "10",
            "_wpsc_special_price": "0",
            "_wpsc_sku": "",
            "_wpsc_stock": "4",
            "unpublish_when_none_left": "1",
            "weight": 8.5,
            "weight_unit": "ounce",
            "height": "0",
            "height_unit": "in",
            "width": "0       ",
            "width_unit": "in",
            "length": "0",
            "length_unit": "in",
            "local": "0",
            "international": "0",
            "no_shipping": "0"
        }
    ],
    "totalCount": "1"
}
Sign up to request clarification or add additional context in comments.

4 Comments

help me to escape this: <span style="color: #ff0000;"> Its not working by backslashes or double backslashes
You shouldn't need to escape JSON strings by hand, you'll likely not cover all cases and you'll end up with responses that will output invalid JSON. How is this JSON getting generated?
As for this specific case, simply need to escape the double quotes: "<span style=\"color: #ff0000;\">". So your JSON should look like: { "key": "<span style=\"color: #ff0000;\">"}
As h--n had said eval cannot parse the \" char: eval("<span style=\"color: #ff0000;\">"). You can execute in console this statement & find the error that "Unexpected identifier <"
0

I tried it in Chrome. Only the following sequence in a JSON string cannot be parsed by eval() in Javascript. Others are fine.

\" \\ \n \r

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.