Chapsterj
Chapsterj

Reputation: 6625

add html tag to json string

Is there a way like in xml to add a html tag to json string data.

Example:

{"title": "bla bla", "copy": "Sed ut perspiciatis unde omnis iste natus <a href="#" class="link">error sit</a> voluptatem accusantium doloremque laudantium"}

In my example I use an a tag to wrap some text. In xml I could just use a ctata tag to wrap the entire text.

UPDATE:

Ok so from some of your answers it looks like Json is not the route to go for data that will have html tags in it. What would be a better solution. My html pages have some simple FAQ links. When the user clicks them it will load the approperate data which would be the title and the description. All I'm trying to do is simply have a user click some FAQ titles and then it will show a popup that will be sent the page ID which I was using as my json IDS. Right now it works great with the json data but I can't add html links to the description data.

For example:

{
 "intro":         [
                    {"title": "title text", "description": "description text1"}, 
                    {"title": "title text", "description": "description text1"}
                  ]
}

In the above example, intro is the page ID. So it can grab this array and find its appropriate title and description. After I did this I noticed some of the copy in the descriptions would have html links in them. Can anyone let me know what would be a better solution to how I setup my external data.

Upvotes: 2

Views: 19188

Answers (5)

Salvatore Leto
Salvatore Leto

Reputation: 56

I know I'm a bit late to the party, but this is what did it for me! The "render" function takes my data in the JSON key "data" and returns the data in "dataname" as well as my desired html tag (span).

                {
                   "data": "dataname", render: function (data) {
                        return data + '<span></span>';
                    }  
                },
                "Other data keys to follow" 
                {
                },

Upvotes: 0

AYOMAH DESTINY
AYOMAH DESTINY

Reputation: 1

If you are trying to put a link in a json string all you need to do is write your link normally as you would in an html but instead of enclosing the href in double quote use single quote. For example

{"name": "<a href='talk.php'>Talk</a>"}

Upvotes: -1

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23332

If you're doing your own JSON serialization (and in most contexts you probably don't want to, but that depends on which langauge you're using), you should start by being familiar with the grammar presented on http://www.json.org/. It tells you right on the front page that double quotes in strings can be escaped with backslashes as \".

Upvotes: 1

Marc B
Marc B

Reputation: 360572

JSON is just a string, so a simple string manipulation would do the trick. You'd have to be VERY careful to not introduce any syntax errors, however. Simply adding <a href="index.php"> would break the string, as the " are JSON metacharacters. you'd have to insert <a href=\"index.php\">, and probably even double-escape those quotes, depending on how you're doing things.

Upvotes: 2

uotonyh
uotonyh

Reputation: 786

You could call the Javascript escape() function before inserting into your JSON. Then call unescape() on the other end.

escape() on W3Schools website

Upvotes: 2

Related Questions