Question

I installed the JMSSerializerbundle with symfony2 to get JSON response of my Doctrine Entities.

The problem is that when I have a string field created by ckeditor and content HTML tags I got an invalid JSON response.

    "courses": [
    {
        "requests": [

        ],
        "id": 38,
        "course_content": "
            pmlllo<\/p>",
        "course_title": "hhjhhhh",
        "course_type": {
            "id": 3,
            "name": "Dershane Dersi",
            "courses": [

            ]
        },
        "description": "

             ppoookokokok<\/p>"
    }
 ]

Using http://jsonlint.com/ I got this error

Parse error on line 154:...
"course_content": "pmlllo</p>",
-----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

Was it helpful?

Solution

You got to encode HTML if you want to store it in JSON. On the server-side (PHP), htmlentities should do the trick. For the client side, please refer to this question: Escaping HTML strings with jQuery.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top