Why does javascript-JS use AJAX to upload a JSON string to the PHP background using POST to become an object?

Source: Internet
Author: User
Tags string back
In PHP, you can use json_decode ($ str) to convert the string back. Why is it a JSON object? Does PHP have default resolution? Direct output is like this. Then the json_decode (, true) is like this.


In PHP, you can use json_decode ($ str) to convert the string back.
Why is it a JSON object? Does PHP have default resolution?

Direct output is like this.

Then the json_decode (, true) is like this.

Reply content:


In PHP, you can use json_decode ($ str) to convert the string back.
Why is it a JSON object? Does PHP have default resolution?

Direct output is like this.

Then the json_decode (, true) is like this.

First, you need to understand what JSON is,JavaScript Object NotationIt refers to the JavaScript Object Notation, which is a lightweight data exchange format based on text and independent of languages. To put it bluntly, it is a string. Because no additional tag is attached, JS can also be directly processed as an object, so it is often used for data exchange.
PHP recognizes the JSON string because the PHP library containsJSON_parserTo resolve
This is like an exchange between two tribes, even though the language is not available. This 'item' is ours.Data

Since javascript can directly process JSON objects, why do the JSON strings obtained by AJAX requests need to be converted to JSON objects?JSON stringAndJSON objectNow
JSON string: a js string that meets the JSON format requirements. As follows:

var jsonStr = "{id:'1',name:'Fini',age:30}";

JSON object: a js object that meets the JSON format requirements. As follows:

var jsonObj = { id: "1", name: "Fini", age: 30 };

In PHP, the related operations are as follows:json_encode,json_decode
json_encodeTheoretically, all data types except resourse can be convertedJSON string(But sometimes it will fail ~ Token ~)

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);var_dump(json_encode($arr));/**** result by php5.6 ****/string '{"a":1,"b":2,"c":3,"d":4,"e":5}' (length=31)

json_decodeIsJSON stringConvert to PHParrayOrobject, Depending on the second parameter (true/false)

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';var_dump(json_decode($json));var_dump(json_decode($json, true));/**** result by php5.6 ****/object(stdClass)[1]  public 'a' => int 1  public 'b' => int 2  public 'c' => int 3  public 'd' => int 4  public 'e' => int 5array (size=5)  'a' => int 1  'b' => int 2  'c' => int 3  'd' => int 4  'e' => int 5

By default, json_decode is converted to an object, and the array is added with ture.

Instructions on json_decode in the PHP Manual
Mixed json_decode (string $ json [, bool $ assoc])
Accept oneJSON stringAnd convert it to a PHP variable.

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.