Detailed analysis of php json instance

Source: Internet
Author: User
Tags bitmask php array to json php json
JSON is a lightweight data interaction format. it can easily and quickly submit network applications. json is less readable than xml, but better than xml in data transmission, let's look at several instances. Encode JSON in PHP (json_en... JSON is a lightweight data interaction format. it can easily and quickly submit network applications. json is less readable than xml, but better than xml in data transmission, let's look at several instances.

JSON (json_encode) encoded 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:

  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:

 name = "sachin";$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:

 

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)

{"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:

 ". $ Jsondata ['translation'] [0]; // Hello worldecho"
". $ Jsondata ['query']; // Hello, world echo"
". $ Jsondata ['web'] [0] ['value'] [0]; // hello worldecho"
". $ Jsondata ['web'] [1] ['key']; // Hello, world?>

Example, combined with database operations

 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.