我想用这个结构创建一个 JSON:
{"id":"ws",
"data":[
{"name":"it.geosolutions"},
{"name":"cite"},
{"name":"testwor"},
{"name":"tiger"},
{"name":"sde"},
{"name":"topp"},
{"name":"newwork"},
{"name":"sf"},
{"name":"nurc"}
]
}
我愿意:
function funcArray(){
foreach ($ws as $item){
$wsarray[] = '{"name":"'.$item->name.'"}';
}
return $wsarray;
}
$json_data = array ('id'=>'ws','data'=>funcArray());
$json = json_encode($json_data);
我得到:
{"id":"ws",
"data":[
"{\"name\":\"it.geosolutions\"}",
"{\"name\":\"cite\"}",
"{\"name\":\"testwor\"}",
"{\"name\":\"tiger\"}",
"{\"name\":\"sde\"}",
"{\"name\":\"topp\"}",
"{\"name\":\"newwork\"}",
"{\"name\":\"sf\"}",
"{\"name\":\"nurc\"}"
]
}
如何修复它?
更新
我试过这个:
function funcArray(){
foreach ($ws as $item){
$wsarray[] = json_encode(array('name'=>$item->name));
}
return $wsarray;
}
但是得到:
{"id":"ws","data":["{\"name\":\"it.geosolutions\"}","{\"name\":\"cite\"}","{\"name\":\"testwor\"}","{\"name\":\"tiger\"}","{\"name\":\"sde\"}","{\"name\":\"topp\"}","{\"name\":\"newwork\"}","{\"name\":\"sf\"}","{\"name\":\"nurc\"}"]}
怎么了?