我的php代码如下:
$query = "SELECT *
FROM `address`
WHERE customer_id =$customer_id
ORDER BY `default` DESC";
$result=mysql_query($query);
$value = mysql_num_rows($result);
if($value>=1)
{
while($row = mysql_fetch_array($result))
{
$details = array(
'status'=>'sucess',
'message'=>'address available',
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
echo json_encode($details);
}
其输出如下:
{"status":"sucess","message":"address available","id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}
它是json对象。我要求json是:
{"status":"sucess","message":"address available","details":[{"id":"52","customer_id":"14","at":"Home","name":"Shhsh","mobile":"99989998","city":"Calicut","address":"Gsggsgs","latlog":"76.3007429,76.3007429","default":"Yes"}]
}
我应该对所需的json格式进行哪些更改?那就是json对象包含json数组。我应该做什么改变才能获得所需的json格式。我是新来的。提前感谢。
答案 0 :(得分:1)
您需要在外部创建一个数组以附加结果数据,然后在将其解码为JSON之前将其放入主数组中。
像这样,
if($value>=1)
{
$details=array();
while($row = mysql_fetch_array($result))
{
$details[]= array(
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
$main_array=array();
$main_array['status']='sucess';
$main_array['message']='address available';
$main_array['details']=$details;
echo json_encode($main_array);
}
这将生成这样的JSON,http://json-parser.com/deec3edd
答案 1 :(得分:0)
请在我认为不需要时删除
$query ="SELECT id,customer_id,at,name,mobile,
city,address,latlog,default
FROM `address`
WHERE customer_id =$customer_id
ORDER BY `default` DESC";
$result=mysql_query($query);
$value = mysql_num_rows($result);
$json=array();
$json['status']='sucess';
$json['message']='address available';
$json['details']=$value;
echo jsone_encode($json);
答案 2 :(得分:0)
将此代码替换为您的代码...它为您工作...谢谢
while($row = mysql_fetch_array($result))
{
$details = array(
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
$json= array('status'=>'sucess','message'=>'address available','details'=> $details);
echo json_encode($json);
答案 3 :(得分:0)
您要做的是生成详细信息数组并将其作为数组添加到响应中,这样您就可以控制循环外的状态(例如,如果它不返回任何数据):
if($value>=1){
$details=array();
while($row = mysql_fetch_array($result))
{
$details[]= array(
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
$main_array=array();
$message = 'address available';
}else{
$message = 'no address';
$details = null;
}
echo json_encode(array(
'success' => $value>=1,
'message' => $message,
'details' => $details
));
答案 4 :(得分:0)
您可以编辑代码
while($row = mysql_fetch_array($result))
{
$details = array(
'status'=>'sucess',
'message'=>'address available',
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
);
}
echo json_encode($details);
到
$datails = array();
while($row = mysql_fetch_array($result))
{
$detail = array(
'status'=>'sucess',
'message'=>'address available',
'details'=>[
'id' => $row['id'],
'customer_id' =>$row['customer_id'],
'at' => $row['at'],
'name'=>$row['name'],
'mobile'=>$row['mobile'],
'city'=>$row['city'],
'address'=>$row['address'],
'latlog'=>$row['latlog'],
'default'=>$row['default']
]
);
array_push($datails,$datail);
}
echo json_encode($details);