A summary of JSON operations in PHP and JavaScript

Source: Internet
Author: User
Tags hash scalar

Nanyi Daniel's Blog mentions:

Structurally, all data can eventually be decomposed into three types:

The first type is a scalar (scalar), a separate string or number (numbers), such as "Beijing", a separate word.

The second type is the sequence (sequence), in which several related data are tied together in a certain order, also called an array or list, such as "Beijing, Shanghai".

The third type is the mapping (mapping), a name/value pair (Name/value), where the data has a name and a corresponding value, which is also known as a hash (hash) or a dictionary (dictionary), such as "Capital: Beijing".

JSON's rules:

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 ("{}").

The above four rules are all in JSON format.

format of JSON in PHP:

{' node ': 1, ' left ': {' node ': 2, ' left ': 4, ' Right ': 5}, ' Right ': 3}

If the JSON block is all array:

[{"City": "\u5929\u6d25", "PM25": "The"},{"City": "\u77f3\u5bb6\u5e84", "PM25": "A"},{"City": "\u4e0a\u6d77", "PM25": " 52 "}]

Common errors with Json_decode ()

$bad _json = "{' Bar ': ' Baz '}";

$bad _json = ' {bar: ' Baz '} ';

$bad _json = ' {' bar ': ' Baz ',} ';

Performing Json_decode () on these three strings will return null and have an error.

The first error is that the JSON delimiterin PHP (delimiter) only allows double quotes and cannot use single quotes. The second error is the name of the JSON name value pair (the part to the left of the colon), which must be used in double quotes in any case. The third error is that you cannot add a comma after the last value (trailing comma)

json in javascript:

<script>

var a = {name: "Tom", Sex: "Mans", Age: "24"}; A is an object

var B = ' {' name ': ' Mike ', ' sex ': ' Woman ', ' age ': ' 29 '} '; B is a string

var a2str = json.stringify (a); Object into a string

var b2obj = Json.parse (b); String into Object

</script>


is actually an object;

The simplest, for example: var a = {"name": "Jack"};

Here, the quotation marks in the name section are not required, and "Jack" is required if written so var a = {Name:jack};

will be an error: Uncaught referenceerror:jack is not defined


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.