json语法
语法:
- 花括号保存对象 { "firstName":"John" , "lastName":"Doe" }
- 方括号保存数组 数组的值有下标则用花括号
$data=[]; []
$data=[1,2]; [1,2]
$data=[1,'age'=>20]; //json { "0": 1, "age": 20 } $data=[ [1,2] ]; //json [[1,2]] $data=[ ['banana','age'=>20] ]; //json [{"0":"banana","age":20}]
$data=[ 1, 'age'=>20, [ 1, 'name'=>'tom', new stdClass(), 'obj'=> new stdClass(), [ 'words', 'type'=>'english' ] ] ]; //json { "0": 1, "1": { "0": 1, "1": { }, "2": { "0": "words", "type": "english" }, "name": "tom", "obj": { } }, "age": 20 }
$data=[ 'articles'=>[ [ "title" =>"高铁严重晚点 损失谁来补偿", "content"=>"文章内容体", ], [ "title" =>"文章标题 最多64个汉字", "content"=>"文章内容体,最多20480个字符", ] ], [ [ "id"=> 293054, "author" =>"思烁哥哥" ], [ "id"=> 293054, "author" =>"作者" ] ], [ "title" =>"文章标题 最多64个汉字", ], ]; //json { "0": [ { "id": 293054, "author": "思烁哥哥" }, { "id": 293054, "author": "作者" } ], "1": { "title": "文章标题 最多64个汉字" }, "articles": [ { "title": "高铁严重晚点 损失谁来补偿", "content": "文章内容体" }, { "title": "文章标题 最多64个汉字", "content": "文章内容体,最多20480个字符" } ] }
$data=new stdClass();
{}
$subData=new stdClass(); $subData->age=20; $data=new stdClass(); $data->name='tom'; $data->obj=$subData; //json {"name":"tom","obj":{"age":20}}
$subData=new stdClass(); $subData->age=20; $subData->type=['math'=>['集合,几何'],'english',1,['lisi','dili']]; $data=new stdClass(); $data->name='tom'; $data->like=['ball',8,'football',new stdClass(),[1,2]]; $data->obj=$subData; //json { "name": "tom", "like": [ "ball", 8, "football", { }, [ 1, 2 ] ], "obj": { "age": 20, "type": { "0": "english", "1": 1, "2": [ "lisi", "dili" ], "math": [ "集合,几何" ] } } }