php生成指定格式的json
请教一下如何用php生成指定格式的json,这个结构比较复杂,数据随便写就行,结构要一样。{"code":0,"msg":"success","reqid":"dd3ff...
请教一下如何用php生成指定格式的json,这个结构比较复杂,数据随便写就行,结构要一样。
{
"code": 0,
"msg": "success",
"reqid": "dd3ff0b2904a94ccc2d6dc3c1573b6a8",
"data": {
"categories": [{
"id": 103532,
"name": "水果类",
"sort": 0,
"visibility": 1,
"pcid": 100001,
"cids": {
"103533": {
"id": 103533,
"name": "国产水果",
"sort": 0,
"visibility": 1,
"pcid": 103532
},
"103534": {
"id": 103534,
"name": "进口水果",
"sort": 1,
"visibility": 1,
"pcid": 103532
},
"103535": {
"id": 103535,
"name": "水果整箱",
"sort": 2,
"visibility": 1,
"pcid": 103532
}
},
"flag": "",
"disabled_show": 0
},
{
"id": 103533,
"name": "酒类",
"sort": 0,
"visibility": 1,
"pcid": 100001,
"cids": {
"103533": {
"id": 103533,
"name": "国产酒",
"sort": 0,
"visibility": 1,
"pcid": 103532
},
"103534": {
"id": 103534,
"name": "进口酒",
"sort": 1,
"visibility": 1,
"pcid": 103532
},
"103535": {
"id": 103535,
"name": "酒整箱",
"sort": 2,
"visibility": 1,
"pcid": 103532
}
},
"flag": "",
"disabled_show": 0
}
],
"products": {
"103532": [{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
},
{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
}
],
"103533": [{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
},
{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
}
]
},
"trackid": "sf"
}
}
对了,数据从mysql中出来的。不要直接写哦 谢谢 展开
{
"code": 0,
"msg": "success",
"reqid": "dd3ff0b2904a94ccc2d6dc3c1573b6a8",
"data": {
"categories": [{
"id": 103532,
"name": "水果类",
"sort": 0,
"visibility": 1,
"pcid": 100001,
"cids": {
"103533": {
"id": 103533,
"name": "国产水果",
"sort": 0,
"visibility": 1,
"pcid": 103532
},
"103534": {
"id": 103534,
"name": "进口水果",
"sort": 1,
"visibility": 1,
"pcid": 103532
},
"103535": {
"id": 103535,
"name": "水果整箱",
"sort": 2,
"visibility": 1,
"pcid": 103532
}
},
"flag": "",
"disabled_show": 0
},
{
"id": 103533,
"name": "酒类",
"sort": 0,
"visibility": 1,
"pcid": 100001,
"cids": {
"103533": {
"id": 103533,
"name": "国产酒",
"sort": 0,
"visibility": 1,
"pcid": 103532
},
"103534": {
"id": 103534,
"name": "进口酒",
"sort": 1,
"visibility": 1,
"pcid": 103532
},
"103535": {
"id": 103535,
"name": "酒整箱",
"sort": 2,
"visibility": 1,
"pcid": 103532
}
},
"flag": "",
"disabled_show": 0
}
],
"products": {
"103532": [{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
},
{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
}
],
"103533": [{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
},
{
"id": 101998,
"name": "水晶富士",
"p_cids": {
"103533": {
"103532": "水果类"
}
},
"cids": {
"103533": "国产水果"
},
"category_id": 103532
}
]
},
"trackid": "sf"
}
}
对了,数据从mysql中出来的。不要直接写哦 谢谢 展开
1个回答
展开全部
先写一个数组,再用 json_encode 转换成 json 即可,如:
<?php
$json = array(
"code" => 0,
"msg" => "success",
"reqid" =>"dd3ff0b2904a94ccc2d6dc3c1573b6a8",
"data" => array(
"categories" => array(
//xxxxxxxxx
),
),
);
echo json_encode($json);
然后就可以了,数据的结构需要你自己补充。
追问
谢谢你的回答,不是直接写哦,数据从mysql出来的。
追答
从mysql读出来也一样,你也要动态的去构造一个array,原理是一样的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询