Understanding of objects and arrays in two JSON structures, json Arrays

Source: Internet
Author: User

Understanding of objects and arrays in two JSON structures, json Arrays

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy to read and write, and easy to parse and generate by machines (generally used to increase the network transmission rate ).

Json is an extremely simple and easy-to-understand data format. The json rules are as follows:

1) Separate the parallel data with commas.

2) The ing is represented by a colon.

3) The set (array) of the parallel data is represented by square brackets.

4) The ing set (object) is represented by braces.

JSON has two types of structure arrays and objects: arrays represent the set of ordered data, and objects represent the set of unordered data.

Take the following example:

$arr = array(111,'aaa','bbb');$arr1 = array('a' => 'aaa','b' => 222);$arr2 = array('a' => 'aaa','other' => array('bbb',1111));echo json_encode($arr); //[111,"aaa","bbb"]echo json_encode($arr1); //{"a":"aaa","b":222}echo json_encode($arr2); //{"a":"aaa","other":["bbb",1111]}

As shown in the preceding figure, because javascript does not support correlated arrays, json_encode () only converts the indexed array (indexed array) to the array format, and converts the associative array (associative array) to the object format.

Json support for PHP:

Json_encode only supports UTF-8-encoded data;

Json_decode always reflects only one PHP Object. If the second parameter is set to true, an array is returned:

$json = '{"a":"aaa","other":["bbb",1111]}';print_r(json_decode($json));print_r(json_decode($json, true)); 

The above is an understanding of the JSON structure objects and arrays introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.