PHP--JSON

Source: Internet
Author: User

<title>Php–json</title> Php–json

http://jsonlint.com/to determine if the JSON syntax is correct

PHP to JSON
$ arr1 Array (); $ arr1 [' username 'test '; $ arr1 [' age '] = 24; $ jsonObj1 = Json_encode ($arr1);  {"username": "Test", "Age":
$arr2=Array();$arr2[' member '][' One '][' username '] =' test1 ';$arr2[' member '][' One '][' age '] = 26;$arr2[' member '][' both '][' username '] =' Test2 ';$arr2[' member '][' both '][' age '] = 28;$JsonObj2= Json_encode ($arr2);//  {"member": {"one": {" username": "Test1", "Age":--}, "one": {"username": "Test2", "age": +}}
class to JSON
class Test{ Public $name="Public Name";protected $Ptname="proctected Name";Private $PName="Private Name"; Public function GetName() {return $ This-name; }}$OBJ=New Test();$JJ= Json_encode ($OBJ);// {"Name": "Public Name"} # only public variable
JSON to PHP variable type
$J=' {' key ': ' Value '} ';$v= Json_decode ($J); Var_dump ($v);// is a class// object (StdClass) [1]//     Public ' key ' = = String ' value ' (length=5)$v= Json_decode ($J,true); Var_dump ($v);// Array Type// Array (size=1)//     ' key ' = = String ' value ' (length=5)
Another complex JSON
"Employees": [    "FirstName":"Bill""LastName":"Gates "},     "FirstName":"George""LastName":"Bush" },    "FirstName":" Thomas "lastName":"Carter" }  ]}
$JSON=' {' Employees ': [{"FirstName": "Bill", "LastName": "Gates"},{"FirstName": "George", "LastName": "Bush"},{"FirstName": "Thomas", "LastName": "Carter"}  ]} ';$arr= Json_decode ($JSON,true);Echo $arr[' Employees '][0][' FirstName '];
JS parsing json
var Jsondata ' {' arr ': [{' Name ': ' NAME1 ', ' id ': ' ID1 '}, {' name ': ' NAME2 ', ' id ': ' ID2 '}, '} '; var jsonobj = Json.parse (jsondata); alert (jsonobj.arr[0].name);         NAME1
$ Members[' 1 '][' username '] =' Admin1 ';$ Members[' 1 '][' groupname '] =' Admin1 ';$ Members[' 1 '][' uid '] =' 1 ';$ Members[' 2 '][' username '] =' admin2 ';$ Members[' 2 '][' groupname '] =' admin2 ';$ Members[' 2 '][' uid '] =' 2 ';p Rint_r (Json_encode ($ Members));//  {"1": {"username": "Admin1", "GroupName": "Admin1", "UID": "1"},//   "2": {"username": "admin2", "GroupName": "Admin2", "UID": "2"},}
Alert (res[1][' username ');      Success alert (res[' 1 '[' username ']);    Success alert (res.1.username);          * failed *alert (res.' 1 '. username);        * Failure *

PHP--JSON

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.