猿问

php转换成json数据格式 ?

{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, 
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, 
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [ 
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, 
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, 
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } 
], 
"musicians": [ 
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, 
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } 
] } 
这样的数据格式是怎么样转换的
我从数据库查出来的数据直接转换的话是
[
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
这样的!

动漫人物
浏览 1050回答 2
2回答

慕的地10843

其实就是把这3个结果并成一个数组,再转就是了例如,$res1=mysql_query($sql1);$res2=mysql_query($sql2);$res3=mysql_query($sql3);定义一个新的数组 $a = array();将3个结果压入这个新的数组array_push($a,$res1);array_push($a,$res2);array_push($a,$res3);最后转义输出echo json_encode($a);LZ遇到问题不要气馁呀

喵喔喔

 json和php数组 格式的互相转换,参考如下:  $php_json = json_encode($json_arr); //把php数组格式转换成 json 格式的数据  echo $php_json;  $php_json = json_decode($php_json); //再把json格式的数据转换成php数组  print_r($php_json);  json数组  $json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的数组转换成 php的数组  $arr = (Array)json_decode($json);  echo $arr[0]->id; //用对象的方式访问
随时随地看视频慕课网APP

相关分类

Java
我要回答