How to Pass Something With Apostrophe to Json

How to pass something with apostrophe to json

use json_encode

$myJSON = array(
'method' => 'feed',
'name' => $tag_line.' at '.$owner_name,
'link' => $short,
'caption' => $description,
'description' => $short
);

$myJSON = json_encode($myJSON);

$myJSON is now contains a string with your data in JSON format and ready to echo to the page.

{
"method" : "feed",
"name" : "tag_line at owner_name",
"link" : "short",
"caption" : "description",
"description" : "short"
}

Single quotes within json value

Use a backslash to escape the character:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);

How to escape double quotes in JSON

Try this:

"maingame": {
"day1": {
"text1": "Tag 1",
"text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
}
}

(just one backslash (\) in front of quotes).

How to escape special characters in building a JSON string?

A JSON string must be double-quoted, according to the specs, so you don't need to escape '.

If you have to use special character in your JSON string, you can escape it using \ character.

See this list of special character used in JSON :

\b  Backspace (ascii code 08)
\f Form feed (ascii code 0C)
\n New line
\r Carriage return
\t Tab
\" Double quote
\\ Backslash character


However, even if it is totally contrary to the spec, the author could use \'.

This is bad because :

  • It IS contrary to the specs
  • It is no-longer JSON valid string

But it works, as you want it or not.

For new readers, always use a double quotes for your json strings.

JSON value with apostrophe

After lots of playing around, I finally got this to work :)

rel='{"id":"#id#","name":"#replace(name,"'","&##39;","all")#"}'

How to escape JSON values with Single Quotes(apostrophe) within JavaScript function

To make a string safe to insert into an HTML attribute:

  • Use htmlspecialchars
  • Use " to delimit the attribute value, or set the option on htmlspecialchars to include ' in the escaped data.

Such:

onclick="UserQueries(<?php echo htmlspecialchars(json_encode($askInfoData)); ?>)" 

Single Quote Doesnt working while pass data to json in android

Change your replaceAll("'","\'"); to replaceAll("'","\\u0027");

EDIT:

\u0027 is the unicode representation of '. Except in rare cases, unicode representations can be used in place of troublesome characters like ',",@,?, and &.

You are likely losing your single quote characters because your json is going through multiple layers of serialization/deserialization that are outside your control.



Related Topics



Leave a reply



Submit