Php json instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Tags bitmask php array to json php json
PHP uses JSON instance analysis. The PHP-encoded JSON (json_encode) PHPjson_encode () function is used in PHPJSON encoding. This function returns the value in JSON format. if it fails, FALSE is returned. Syntax: stringjson_encode ($ va encoded JSON (json_encode) in PHP)

The PHP json_encode () function is used for JSON encoding in PHP. This function returns the value in JSON format. if it fails, FALSE is returned.

Syntax:

String json_encode ($ value [, $ options = 0]) parameter:

Value: The value to be encoded. this function applies only to data encoded by the UTF-8.

Options: this optional value is a bitmask consisting of JSON_HEX_TAG JSON_HEX_QUOT, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, response, JSON_FORCE_OBJECT

Example

The following example demonstrates how to convert a PHP array to JSON:

The code is as follows:

$ Arr = array ('a' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5 );
Echo json_encode ($ arr );
?>

During execution, the following results are generated:

{"A": 1, "B": 2, "c": 3, "d": 4, "e": 5}

The following example shows how to convert a PHP object to JSON:

The code is as follows:

Class Emp {
Public $ name = "";
Public $ hobbies = "";
Public $ birthdate = "";
}
$ E = new Emp ();
$ E-> name = "running in ";
$ E-> hobbies = "sports ";
$ E-> birthdate = date ('m/d/Y h: I: s A', "8/5/1974 12:20:03 p ");
$ E-> birthdate = date ('m/d/Y h: I: s A', strtotime ("8/5/1974 12:20:03 "));

Echo json_encode ($ e );
?>

During execution, the following results are generated:

{"Name": "coming in", "hobbies": "sports", "birthdate": "08/05/1974 12:20:03 "}

Decoding JSON in PHP (json_decode)

PHP json_decode () function is used to decode JSON in PHP. The return value of this function is decoded from json to the appropriate PHP type.

Syntax:

Mixed json_decode ($ json [, $ assoc = false [, $ depth = 512 [, $ options = 0]) parameter:

Json_string: it must be a UTF-8-encoded data-encoded string

Assoc: when this is a Boolean parameter set to TRUE, the returned object will be converted to an associated array.

Depth: it is an integer parameter that specifies the recursive depth

Options: JSON decoding of a bitmask of the integer type. JSON_BIGINT_AS_STRING is supported.

Example

The following example shows how to use PHP to decode JSON objects:

The code is as follows:

$ Json = '{"a": 1, "B": 2, "c": 3, "d": 4, "e": 5 }';

Var_dump (json_decode ($ json ));
Var_dump (json_decode ($ json, true ));
?>

During execution, the following results are generated:

Object (stdClass) #1 (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}

Array (5 ){
["A"] => int (1)
["B"] => int (2)
["C"] => int (3)
["D"] => int (4)
["E"] => int (5)
}

Assume that the JSON data we obtain is as follows: (you can use curl, fsockopen, and so on)

The code is as follows:

{
"Translation": ["Hello world"],
"Query": "Hello World ",
"ErrorCode": 0,
"Web ":[
{
"Value": ["hello world"],
"Key": "Hello World"
},
{
"Value": ["Hello World"],
"Key": "Hello World"
}
]
}

Use the json_decode function to return the array:

Array
(
[Translation] => Array
(
[0] => Hello world
)
[Query] => Hello world
[ErrorCode] => 0
[Web] => Array
(
[0] => Array
(
[Value] => Array
(
[0] => hello world
)
[Key] => Hello world
)
[1] => Array
(
[Value] => Array
(
[0] => Hello World
)
[Key] => Hello world
)
)
)

We can use the following methods in PHP to obtain the desired value:

The code is as follows:

/*----------------------------------
$ Data ='
{
"Translation": ["Hello world"],
"Query": "Hello World ",
"ErrorCode": 0,
"Web ":[
{
"Value": ["hello world"],
"Key": "Hello World"
},
{
"Value": ["Hello World"],
"Key": "Hello World"
}
]
}
';
-------------------------------------*/
$ Data = < {
"Translation": ["Hello world"],
"Query": "Hello World ",
"ErrorCode": 0,
"Web ":[
{
"Value": ["hello world"],
"Key": "Hello World"
},
{
"Value": ["Hello World"],
"Key": "Hello World"
}
]
}
STR;
$ Jsondata = json_decode ($ data, true );
Header ("Content-Type: text/html; charset = UTF-8 ");
// Print_r ($ jsondata );
Echo"
". $ Jsondata ['translation'] [0]; // Hello world
Echo"
". $ Jsondata ['query']; // Hello, world
Echo"
". $ Jsondata ['web'] [0] ['value'] [0]; // hello world
Echo"
". $ Jsondata ['web'] [1] ['key']; // Hello, world
?>

Example, combined with database operations

The code is as follows:

Include './include/conn. php'; // Database link file
$ SQL _notice = mysql_query ('select * FROM gg_notice where enable = "1" limit 0, 10 ');
$ Notice = mysql_fetch_array ($ SQL _notice, MYSQL_ASSOC );
Print_r ($ notice );
?>



Tutorial provided by the first php network-generate the data read by the database in json format


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.