我得到的JSON格式是:
{
"test":[
{"key1":"value1"},
{"key2":"value2"}
]
}但是有没有可能得到这种格式呢?
{
"test": {
"key1":"value1",
"key2":"value2"
}
}我的php代码是这样的:
$key=$row[1];
$value=$row[2];
$posts[] = array($key => $value);
$response['strings'] = $posts;
fwrite($out, json_Encode($response))我已经被困在这里好几个小时了,谁来帮帮忙!提前感谢!
发布于 2012-07-12 10:18:05
你想要的
$posts[$key] = $value;问题在于,具有字符串键的PHP数组在JSON术语中是对象。
发布于 2012-07-12 10:18:09
第一个是数组,第二个是对象。
$posts = new stdClass();
$posts->key1 = "value1";
$posts->key2 = "value2";
$response['strings'] = $posts;
fwrite($out, json_Encode($response))发布于 2012-07-12 10:18:15
我假设您的代码如下所示:
$posts = array();
while( somthing )
{
$row = ...
$key=$row[1];
$value=$row[2];
$posts[] = array($key => $value);
}
$response['strings'] = $posts;
fwrite($out, json_Encode($response))你的解决方案是这样做:
$posts = array();
while( somthing )
{
$row = ...
$key=$row[1];
$value=$row[2];
$posts[$key] = $value;
}
$response['strings'] = $posts;
fwrite($out, json_Encode($response))https://stackoverflow.com/questions/11444171
复制相似问题