Array of JSON data types and object depth analysis

Source: Internet
Author: User
Tags arrays json string to json

In JSON, there are two types of structures: objects and arrays.

1. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma). The name is enclosed in quotation marks; If the value is a string, it must be in parentheses, and the numeric type does not need


Son is an exceptionally straightforward data format, and the JSON rules are just as follows:

1 The data is separated by commas (",").
2) The mapping is represented by a colon (":").
3 the Set (array) of parallel data is represented by square brackets ("[]").
4 the Mapped collection (object) is represented by braces ("{}").

For example:

The code is as follows Copy Code

{"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": ", Topplaytime": "2009-08-20"}

2. An array is an ordered set of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value.
For example:

The code is as follows Copy Code

var jsonranklist=[{"Xlid": "Cxh", "Xldigitid": 123456, "Topscore": ", Topplaytime": "2009-08-20"},{"Xlid": "Zd", " Xldigitid ": 123456," Topscore ": 1500," Topplaytime ":" 2009-11-20 "}];

JSON has two structure arrays and objects: An array represents a collection of ordered data, while an object represents a collection of unordered data.
Look at the following example:

  code is as follows copy code

$arr = array (A, ' AAA ', ' BBB ');
 
$arr 1 = array (
    ' a ' => ' aaa ',
    ' B ' => 222
);
&nb Sp
$arr 2 = array (
    ' a ' => ' aaa ',
    ' other ' => array (
  &N bsp;     ' BBB ', 1111
   )
);
Echo Json_encode ($arr);//[111, "AAA", "BBB"]
 
Echo Json_encode ($arr 1);//{"a": "AAA", "B": 222}
&NBSP
Echo Json_encode ($arr 2)//{"a": "AAA", "Other": [BBB, 1111]}

From the above: because JavaScript does not support associative arrays, json_encode () only converts an indexed array (indexed array) into an array format and an associative array (associative array) into object format.

About PHP support for JSON description:
Json_encode only supports UTF-8 encoded data;
Json_decode always reflects only one PHP object, with the second argument true to return an array:

The code is as follows Copy Code

$json = ' {' A ': ' AAA ', ' other ': [BBB ', 1111]} ';

Print_r (Json_decode ($json));

Print_r (Json_decode ($json, true));

JSON string converted to JSON object

To use the above str1, you must first convert to a JSON object by using the following method:

  code is as follows copy code

//converted from JSON string to JS On object

var obj = eval (' + str + '); (JS has its own)

or

var obj = Str.parsejson (); or

var obj = json.parse (str);//convert from JSON string to JSON object

And then read as follows:

Alert (obj.name);

Alert (obj.sex);

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.