PHP JSON
What is JSON?
· JSON refers to JavaScript Object Notation
· JSON is a lightweight text data exchange format
· JSON is language independent *
· JSON is self-describing and easier to understand
* JSON uses JavaScript syntax to describe data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries support many different programming languages.
Environment configuration
##has been built-in in php5.2.0 and above versions JSON extension.
JSON function
##json_encode
json_encode() is used to JSON encode variables. This function returns JSON data if executed successfully, otherwise it returns FALSE.
Syntax
##json_encode($value,[,options = 0 ] )
Parameter
##· value : The value to encode. This function is only valid for UTF-8 encoded data.
Example
The following example demonstrates How to convert PHP array to JSON format data:
Program running result:
{"a":1,"b": 2,"c":3,"d":4,"e":5}
The following example demonstrates how to convert PHP objects into JSON format data
name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('Y-m-d h:i:s a', "2016/9/19 12:20:03 p"); $e->birthdate = date('Y-m-d h:i:s a', strtotime("2016/9/19 12:20:03")); echo json_encode($e); ?>Program running results:
{"name":"sachin","hobbies":"sports","birthdate":"2016-09-19 12:20:03 pm"} $json [,$assoc = false [, $depth = 512 [, $options = 0 ]]]
json_decode
json_decode() function is used to decode JSON format strings and convert them into PHP variables.
Syntax
json_decode($json [,$assoc = false [, $depth = 512 [, $ options = 0 ]]])
Parameters
##·json_string: to be decoded JSON string, must be UTF-8 encoded data
·assoc: When this parameter is TRUE, an array will be returned, and when FALSE, an object will be returned.
·depth: Integer type parameter, which specifies the recursion depth
·options: Binary mask, currently only JSON_BIGINT_AS_STRING is supported.
json_decode() common Error
The following three ways of writing json are all wrong. Can you see where the error is?$bad_json = "{ 'bar': 'baz' }";$bad_json = '{ bar: "baz" }';
$bad_json = '{ "bar": "baz", }';
Executing json_decode() on these three strings will return null and report an error.
The first mistake is that the json delimiter (delimiter) only allows the use of double quotes, not single quotes.
The second mistake is that the "name" (the part to the left of the colon) of the json name-value pair must use double quotes under any circumstances.
The third error is that you cannot add a comma (trailing comma) after the last value.
In addition, json can only be used to represent objects and arrays. If json_decode() is used on a string or value, null will be returned.
Example
The following example demonstrates How to decode JSON data:
Program execution result:
##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)}
||1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?>
- Course Recommendations
- Courseware download
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watchingElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watchingIntermediateIssue 22_Comprehensive actual combat
5521 people are watchingElementaryIssue 22_PHP Programming
5172 people are watchingElementaryIssue 22_Front-end development
8713 people are watchingIntermediateBig data (MySQL) video tutorial full version
4525 people are watchingElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watchingElementaryGO Language Core Programming Course
2814 people are watchingIntermediateJS advanced and BootStrap learning
2563 people are watchingIntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watchingIntermediateRedis+MySQL database interview tutorial
2963 people are watchingElementaryDeliver food or learn programming?
5708 people are watchingThe courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
#Function | Description |
Pair variables Encode JSON | |
Decode the string in JSON format and convert it into a PHP variable | |
Return the last error that occurred |