我正在尝试在我的jSON输出中获取此格式,但似乎无法弄清楚如何降低格式。
{
"total": 20,
"result": [
{
"image": "http://test.com/images/001.jpg",
"width": 192,
"height": 288
},
{
"image": "http://test.com/images/001.jpg",
"width": 192,
"height": 257
},
{
"image": "http://test.com/images/001.jpg",
"width": 192,
"height": 288
},
{
"image": "http://test.com/images/001.jpg",
"width": 192,
"height": 288
}
]
}
我能够做到这一点,但不包括总数:
$json['result'][] = array('image' => 'http://www.google.com', 'width' => '120', 'height' => '200');
json_encode($json);
我的最终目标是能够从此数据库调用创建json输出:
$objDB = new DB;
$submissions = $objDB
->setStoredProc( 'petContestFetchImages' )
->setParam("offset", $offset )
->setParam("rows", $limit )
->execStoredProc()
->parseXML();
//$link = $submissions->link;
//$width = $submissions->width;
//$heigth = $submissions->height;
//$total = $submissions->total;
修改
这是我的数据库尝试,但jSON响应似乎已关闭..
//Fetch Entries
$objDB = new DB;
$submissions = $objDB
->setStoredProc( 'petContestFetchImages' )
->setParam("offset", $offset )
->setParam("rows", $limit )
->execStoredProc()
->parseXML();
//Total
$json['total'] = $submissions->total;
//Loop over the result set and store it as jSON
foreach($submissions->submission as $submission) {
$json['result'][] = array('petName' => $submission->petName, 'petCaption' => $submission->petCaption, 'imageName' => $submission->imageName);
}
//Output the result
print json_encode($json);
输出:
{
"total": 20,
"result": [
{
"petName": {
"0": "Nala"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "nalaHUS123.png"
}
},
{
"petName": {
"0": "Simba"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "simbaHUS123.png"
}
},
{
"petName": {
"0": "BoBo"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "boboHUS123.png"
}
},
{
"petName": {
"0": "Frank"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "frankHUS123.png"
}
},
{
"petName": {
"0": "Jim"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "frankHUS123.png"
}
},
{
"petName": {
"0": "Sasha"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "boboHUS123.png"
}
},
{
"petName": {
"0": "Sam"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "simbaHUS123.png"
}
},
{
"petName": {
"0": "Alice"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "nalaHUS123.png"
}
},
{
"petName": {
"0": "Sara"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "boboHUS123.png"
}
},
{
"petName": {
"0": "Judy"
},
"petCaption": {
"0": "Shes a wonder pup!"
},
"imageName": {
"0": "nalaHUS123.png"
}
}
]
}
答案 0 :(得分:0)
$json['total'] = 20;
$json['result'][] = array('image' => 'http://www.google.com', 'width' => '120', 'height' => '200');
json_encode($json, JSON_PRETTY_PRINT);
答案 1 :(得分:0)
你需要创建一个数组,其中包含总字符串和结果数组
$json[] = array('image' => 'http://www.google.com', 'width' => '120', 'height' => '200');
$result = array('total'=>20,'result'=>$json);
json_encode($result);
答案 2 :(得分:0)
首先从数据库中获取数据。这可能无法准确输出您需要的JSON,但这将为您提供解决方案。
$result = array();
while($row = mysli_fetch_assoc($query))
{
$result[] = array('image'=>$row['image'],'height',=>$row['height'],'width'=>$row['width']);
}
$data = array( 'total'=>count($result),'result'=>$result);
json_encode($data);