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 onhtmlspecialchars
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
How to Convert Text to Svg Paths
How to Fix Undefined Offset: 0 in Laravel Blade
Align Text and Image Side to Side With Bootstrap
Cron Job to Delete Files Created Before a Specific Time
Php - Mysqli_Fetch_Array() Expects Parameter 1 to Be Mysqli_Result, String Given
Regex for No More Than 5 Digits or Contain String
Php How to Determine the First and Last Iteration in a Foreach Loop
How to Echo Selected Value of Dropdown in PHP
Display Only the Current User'S Details Using PHP and MySQL
How to Add a Space Between Every Sequence of Four Characters (Like a Credit Card Number)
How to Get the Url of Currently Opened Tabs in All Browsers With PHP or JavaScript
How to Show Checkboxes as Checked When Values Are Set in the Database in Laravel
How to Get Foreach Loop Variable Outside of Loop
Implode a Column of Values from a Two Dimensional Array