JSON
Tutorials
<body>
<p>
Name: <span id= "Jname" ></span><br/>
Age: <span id= "Jage" ></span><br/>
Address: <span id= "Jstreet" ></span><br/>
Phone: <span id= "Jphone" ></span><br/>
</p>
<script>
var jsonobject= {
"Name": "John Johnson",
"Street": "Oslo West 555",
"Age": 33,
"Phone": "555 1234567"};
document.getElementById ("Jname"). Innerhtml=jsonobject.name
document.getElementById ("Jage"). Innerhtml=jsonobject.age
document.getElementById ("Jstreet"). Innerhtml=jsonobject.street
document.getElementById ("Jphone"). Innerhtml=jsonobject.phone
</script>
</body>
The JSON data is accessed in josnname format. Jsondata
JSON syntax is a subset of the JavaScript object notation syntax.
- data in name /value pairs
- Data is separated by commas
- Curly braces Save Object
- Square brackets Save Array
JSON name/value pairs
The writing format for JSON data is: name/value pairs.
name /value pairs include the field name (in double quotation marks), followed by a colon, and then the value:
"FirstName": "John"
This is easy to understand, equivalent to this one JavaScript statements:
FirstName = "John"
JSON Value
The JSON value can be:
- Number (integer or floating point)
- String (in double quotes)
- logical value (true or false)
- Array (in square brackets)
- Object (in curly braces)
- Null
JSON Object
The JSON object is written in curly braces:
An object can contain multiple name /value pairs:
{"FirstName": "John", "LastName": "Doe"}
This is also easy to understand, with this article JavaScript statements are equivalent:
FirstName = "John"
LastName = "Doe"
JSON Array
The JSON array is written in square brackets:
An array can contain multiple objects:
{
"Employees": [
{"FirstName": "John", "LastName": "Doe"},
{"FirstName": "Anna", "LastName": "Smith"},
{"FirstName": "Peter", "LastName": "Jones"}
]
}
in the example above, the object "Employees" is an array that contains three objects. Each object represents a record of a person (with a first and last name).
JSON uses JavaScript syntax
because JSON uses JavaScript syntax, so you can work with JSON in JavaScript without the need for additional software.
through JavaScript, you can create an array of objects and assign values like this:
Example
var employees = [
{"FirstName": "John", "LastName": "Doe"},
{"FirstName": "Anna", "LastName": "Smith"},
{"FirstName": "Peter", "LastName": "Jones"}
];
can be accessed like this The first item in an array of JavaScript objects:
Employees[0].lastname;
The returned content is:
Doe
You can modify the data like this:
Employees[0].firstname = "Jonatan";
JSON File
- The file type of the JSON file is ". JSON"
- The MIME type of JSON text is "Application/json"
PHP Operation JSON
JSON extensions are already built into the php5.2.0 and above versions.
JSON Functions
Function |
Describe |
Json_encode |
JSON Encoding of variables |
Json_decode |
Decodes a JSON-formatted string into a PHP variable |
Json_last_error |
Returns the last error that occurred |
Json_encode
PHP Json_encode () is used to encode the variable JSON, which returns FALSE if the execution succeeds in returning the JSON data.
Grammar
String Json_encode ($value [, $options = 0])
Parameters
- Value: The values to encode. This function is valid only for UTF-8 encoded data.
- options: A binary mask consisting of the following constants: It's too messy to remember.
The following example demonstrates how to add a The PHP array is converted to JSON-formatted data:
<?php
$arr = Array (' a ' = = 1, ' b ' = = 2, ' c ' = 3, ' d ' = = 4, ' e ' = 5);
echo Json_encode ($arr);? >
The result of the above code execution is:
{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
The following example demonstrates how to add a PHP objects are converted to JSON-formatted data:
<?php
Class Emp {
Public $name = "";
Public $hobbies = "";
Public $birthdate = "";
}
$e = new Emp ();
$e->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);? >
The result of the above code execution is:
{"Name": "Sachin", "hobbies": "Sports", "Birthdate": "08\/05\/1974 12:20:03 PM"}
Json_decode
The PHP Json_decode () function is used to decode a JSON-formatted string and convert it to a PHP variable.
Grammar
Mixed Json_decode ($json [, $assoc = False [, $depth = [] [, $options = 0]]])
Parameters
json_string: JSON string to decode, must be UTF-8 encoded data
Assoc: When this argument is TRUE, the array is returned, FALSE when the object is returned.
depth: An integer-type parameter that specifies the recursive depth
options: Binary mask, currently only supports json_bigint_as_string.
PHP writes and reads JSON files
<?php
//Generate a PHP array pretend to take data from the front end or anywhere else
$data = array();
$data [' name '] = ' test1 ';
$data [' work '] = ' process ';
$data [' purpose '] = ' json ';
json_encode-JSON encodes a variable to encode value, in addition to the resource type, can be any data type
The function can only accept UTF-8 encoded data (refers to the character/string type of data)
If the encoding succeeds, it returns a string in JSON form.
First, convert the array into JSON
$json _string = Json_encode ($data);
Write file
File_put_contents (' Test.json ', $json _string);
<?php
//Read data from a file, in a string form into PHP
$json _string = file_get_contents (' Test.json ');
json_decode-decoding of strings in JSON format
Mixed Json_decode (String $json [, bool $assoc])
Accept a JSON-formatted string and convert it to a PHP variable
$assoc When this argument is TRUE, an array is returned instead of an object.
Convert JSON string to PHP array
$data = Json_decode ($json _string,true);
Var_dump ($data);
Learning from JSON data