JSON 编码
知识概述
json 数据格式:主要由对象 { }
和数组 [ ]
组成:
其中对象包括键值对(属性:
属性值){key:value}
,value
可为 str
,num
,list
,obj
;
取值以 objcet.key
获取;
对象 {key:value, key2:value2,}
键 :
值 以 冒号分开,对间 以 ,
连接;
数组包含元素:str
,num
,list
,obj
都可以,利用索引访问 [index]
,用 .
连接各个值。
数组
var stu = {
"student":
[
{"name": "Tom","Grade":1, "age":11, "gender": "M"},
{"name": "Jerry", "Grade":1, "age":10, "gender": "M"}
],
"position":
{"class1": "room1", "class2": "room2"}
};
取值
document.write(stu.student[1].name); // 输出第二个学生名 "Jerry"
document.write(stu.student[0].age); // 输出第一个学生年龄 "11"
document.write(stu.position.class1); // 输出 position 的 class1 值 "room1"
document.write(stu["position"].class2); // 同时支持中括号键访问对象值 "room2"
类型简述
json_encode 编码常见用于索引类型和关联类型数组。
其中索引类型又分为数组 ["val1","val2",...]
和对象 {"0":"one","1":"two",...}
,从 0
开始为每个对象索引序号;
而关联类型为可以为数值指定名称 {"KR":"Korea","JP":"Japan","SG":"Singapore"}
,可以更直观判断值的对应关系。
索引数组
默认仅以数组形式输出
$arr = Array('one', 'two', 'three');
echo json_encode($arr);
输出结果 https://static.hexingxing.cn/v2/example/jsonext/JSON_ARRAY_ONLY_VALUE/
格式:[]
["one","two","three"]
索引对象
JSON_FORCE_OBJECT
强制转换为对象输出
$arr = Array('one', 'two', 'three');
echo json_encode($arr,JSON_FORCE_OBJECT);
输出结果 https://static.hexingxing.cn/v2/example/jsonext/JSON_ARRAY_INDEX/
格式:{}
{"0":"one","1":"two","2":"three"}
关联数组
$arr = Array('KR'=>'Korea', 'JP'=>'Japan', 'SG'=>'Singapore');
echo json_encode($arr);
输出结果 https://static.hexingxing.cn/v2/example/jsonext/JSON_ARRAY_ASSOCIATIVE/
格式:{}
{"KR":"Korea","JP":"Japan","SG":"Singapore"}
对象转换
$obj->body = 'another post';
$obj->id = 21;
$obj->approved = true;
$obj->favorite_count = 1;
$obj->status = NULL;
echo json_encode($obj,JSON_PRETTY_PRINT);
输出结果 https://static.hexingxing.cn/v2/example/jsonext/JSON_ARRAY_OBJECT_CONVERSION/
格式:{}
{
"body":"another post",
"id":21,
"approved":true,
"favorite_count":1,
"status":null
}
JSON 参数
JSON_PRETTY_PRINT
格式化输出JSON_FORCE_OBJECT
强制转换为对象JSON_UNESCAPED_UNICODE
支持中文字符
默认数组形式
$水果 = [
"苹果",
"西瓜",
"香蕉"
];
header('Content-type:application/json;charset=utf-8');
echo json_encode($水果);//默认
["\u82f9\u679c","\u897f\u74dc","\u9999\u8549"]
https://static.hexingxing.cn/v2/example/jsonext/JSON_NONE/
格式化输出对象
$水果 = [
"a"=>"苹果",
"b"=>"西瓜",
"c"=>"香蕉"
];
header('Content-type:application/json;charset=utf-8');
echo json_encode($水果, JSON_PRETTY_PRINT);//格式化输出对象
{
"a": "\u82f9\u679c",
"b": "\u897f\u74dc",
"c": "\u9999\u8549"
}
https://static.hexingxing.cn/v2/example/jsonext/JSON_PRETTY_PRINT/
强制转换为对象
$水果 = [
"苹果",
"西瓜",
"香蕉"
];
header('Content-type:application/json;charset=utf-8');
echo json_encode($水果, JSON_FORCE_OBJECT);//强制转换为对象
{"0":"\u82f9\u679c","1":"\u897f\u74dc","2":"\u9999\u8549"}
https://static.hexingxing.cn/v2/example/jsonext/JSON_FORCE_OBJECT/
支持显示中文字符
$水果 = [
"a"=>"苹果",
"b"=>"西瓜",
"c"=>"香蕉"
];
header('Content-type:application/json;charset=utf-8');
echo json_encode($水果, JSON_UNESCAPED_UNICODE);//支持显示中文字符
{"a":"苹果","b":"西瓜","c":"香蕉"}
https://static.hexingxing.cn/v2/example/jsonext/JSON_UNESCAPED_UNICODE/
格式化输出并支持中文字符
$水果 = [
"a"=>"苹果",
"b"=>"西瓜",
"c"=>"香蕉"
];
header('Content-type:application/json;charset=utf-8');
echo json_encode($水果, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);//格式化输出并支持中文字符
{
"a": "苹果",
"b": "西瓜",
"c": "香蕉"
}
https://static.hexingxing.cn/v2/example/jsonext/JSON_PRETTY_PRINT&JSON_UNESCAPED_UNICODE/
0 条评论