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