We all know there are three kinds of data from Ajax, text, Jason,xml
I now want him to pass back the data type four Jason
I define this in the controller:
$a = 10;
$Jason = ' {' number ': ' $a '} '
Why can't you write like that? He returned a value of $ A instead of 10?
Some say you put 10 in it, I understand, but what if you take the value out of the database?
For example
$a = $row [' content '];
echo $a;//This can be taken out of the database value
$Jason = ' {' content ': ' $a '} ';//this is not possible;
If I write $jason= ' {' content ': ' $row [' content '] '} ';//is to replace $ A directly with $row[' content ';
He will prompt for grammatical errors;
Can't the value in Jason be replaced with a variable?
If it can't be replaced, then how to remove the value from the database, the above $jason format how to change ah?
Reply to discussion (solution)
JSON is not returned in the form of an array, can you use variables?
$Jason = ' {' content ': ' $a '} ';
/This is not possible because the variables in single quotes are not replaced by values.
Can write
$Jason = "{\" content\ ": \" $a \ "}";
Or
$Jason = sprintf (' {' content ': '%s '} ', $a);
In fact, PHP has provided the Json_encode function, you can easily convert a variety of variables into a JSON format string
;
Try $jason= ' {' number ': '. $a. '} '
Single quotation marks do not resolve variables and most escapes, only double quotation marks can be
Directly using PHP's Json_encode () to leverage the
Directly using PHP's Json_encode () to leverage the
There are 2 problems, the page encoding GBK, the results shown with Json_encode () is empty? What's the deal?
The 2nd one uses the Json_encode () Chinese character not to display normally, to JS's display is the binary number
Directly using PHP's Json_encode () to leverage the
There are 2 problems, the page encoding GBK, the results shown with Json_encode () is empty? What's the deal?
The 2nd one uses the Json_encode () Chinese character not to display normally, to JS's display is the binary number
The JSON data for AJAX is dynamic I data, so that the database can return utf-8 data.
Any encoded data to the browser has been converted to Unicode encoding, Json_encode first done, and nothing bad
You can use PHP's Json_encode. If not UTF8 data, you can use Iconv (' GBK ', ' UTF ', $content); Turn around and then Json_encode.
$a =10; $arr = Array (' number ' = $a); $Jason = Json_encode ($arr); {"Number": 10}echo $Jason;
$a =10; $arr = Array (' content ' = = $a); $Jason = Json_encode ($arr); {"Content": 10}echo $Jason;