2016-07-26 80 views
0

我正在为iOS应用程序构建一个API,并试图将mySQL数据转换为JSON字符串进行处理。所需的输出将需要顶级订单详细信息,如客户名称和地址,然后订购产品的子阵列。在PHP中嵌套的JSON输出

在我需要的两张表中都有相当多的字段,我希望能够拥有所有字段。我已经构建了一个脚本来执行此操作,但输出报告为JSON验证器中的格式不正确。

这里是我的代码:

$orders = $db->get_results("SELECT ords.serial as ID, cust.title, cust.name, cust.surname, cust.address, cust.address2, cust.town, cust.county, cust.postcode, cust.phone, cust.height, cust.weight, cust.houseType, cust.parking, cust.access, ords.furnitureRem, ords.furnitureRemDetails, ords.comments FROM orders as ords JOIN customers as cust ON ords.customerid = cust.serial JOIN order_detail as odeet ON ords.serial = odeet.orderid JOIN `user` as us ON us.id = ords.user_id WHERE us.id='".$_REQUEST['userID']."' GROUP BY ords.serial ORDER BY cust.serial DESC"); 

$json_response = array(); //Create an array 
foreach ($orders as $row) 
{ 
    $row_array = array(); 
    $row_array[] = $row;   
    $ord_id = $row->ID; 

    $orders2 = $db->get_results("SELECT * FROM order_detail as ord 
    JOIN products as prod ON ord.productid = prod.id 
    WHERE ord.orderid = ".$ord_id); 
    foreach ($orders2 as $vorder2) { 
    { 
     $row_array['products'][] = $vorder2; 
    } 
    array_push($json_response, $row_array); //push the values in the array 
} 
echo json_encode($json_response); 
} 

的电流输出是这样的:

enter image description here

这里的原始输出,通过要求:

[{"0":{"ID":"756","title":"Mr","name”:”John”,”surname”:”Smith”,”address”:”Address Line 1”,”address2”:”Address Line 2”,”town”:”Town","county”:”County”,”postcode”:”PO57 8DE”,”phone":"0777777777777”,”height":"6ft","weight":"14st","houseType":"Flat","parking":"none","access":"No problems","furnitureRem":"0","furnitureRemDetails":null,"comments":null},"products":[{"orderid":"756","productid":"2","price":"6500","status":"1","id":"2","type":"Chair","style":"Surrey","action":"Single Motor 2 Way Tilt in Space","weightCap":"35st","height":"14\"","width":"24\"","internalWidth":null,"length":null,"internalLength":null,"depth":"16\"","fabric":"Leather","fabricCode":"LR43","backStyle":"Waterfall","cushionFabric":"Leather","cushionFabricCode":"LR43","backHeight":"32\"","armHeight":"7\"","seatingOptions":"Memory Foam","armOption":"Scrolled","backupBattery":"2x Backup","headRoll":"1","headCover":"1","seatCover":"0","armCover":"0","armCap":"1","pocket":"Left","loop":"Left","antimacassar":"0","freedom":"1","heatMassage":"0","castorsGlides":"Castors","footPlate":"0","seatDepthAdj":"0","doorFrame":null,"additionalCover":"0","scViscoform":"0","scPommelViscoform":"0","scLiquiform":"0","scPommelLiquiform":"0","scCelliform":"0","scAirform":"0","scDynaform":"0","bsWaterfall":"0","bsComfortLateral":"0","bsProfileWaterfall":"0","bsProfileComfortLateral":"0","bsSupportLateral":"0","bsProfileSupportLateral":"0","hsLargeProfileHeadrest":"0","hsSmallHeadPillow":"0","hsSmallProfileHeadPillow":"0","hsMidlineHeadrest":"0","woodColour":"0","frameColour":null,"baseColour":null,"headFootBoard":null,"mattressType":null,"productHeightLowest":null,"productHeightHighest":null,"liftingPoles":null,"grabRails":null,"readingLight":"0","existingBedHeight":null,"legHeight":null,"drawerOptions":null,"hoistCutouts":"0","cotSides":null,"liftingPole":"0","sideRetention":"0","linked":"0","rrp":null,"stock":"0"}]}][{"0":{"ID":"756","title":"Mr","name":"John","surname":"Smith","address":"Address Line 1","address2":"Address Line 2","town":"Town","county":"County","postcode":"PO57 8DE","phone":"0777777777777","height":"6ft","weight":"14st","houseType":"Flat","parking":"none","access":"No problems","furnitureRem":"0","furnitureRemDetails":null,"comments":null},"products":[{"orderid":"756","productid":"2","price":"6500","status":"1","id":"2","type":"Chair","style":"Surrey","action":"Single Motor 2 Way Tilt in Space","weightCap":"35st","height":"14\"","width":"24\"","internalWidth":null,"length":null,"internalLength":null,"depth":"16\"","fabric":"Leather","fabricCode":"LR43","backStyle":"Waterfall","cushionFabric":"Leather","cushionFabricCode":"LR43","backHeight":"32\"","armHeight":"7\"","seatingOptions":"Memory Foam","armOption":"Scrolled","backupBattery":"2x Backup","headRoll":"1","headCover":"1","seatCover":"0","armCover":"0","armCap":"1","pocket":"Left","loop":"Left","antimacassar":"0","freedom":"1","heatMassage":"0","castorsGlides":"Castors","footPlate":"0","seatDepthAdj":"0","doorFrame":null,"additionalCover":"0","scViscoform":"0","scPommelViscoform":"0","scLiquiform":"0","scPommelLiquiform":"0","scCelliform":"0","scAirform":"0","scDynaform":"0","bsWaterfall":"0","bsComfortLateral":"0","bsProfileWaterfall":"0","bsProfileComfortLateral":"0","bsSupportLateral":"0","bsProfileSupportLateral":"0","hsLargeProfileHeadrest":"0","hsSmallHeadPillow":"0","hsSmallProfileHeadPillow":"0","hsMidlineHeadrest":"0","woodColour":"0","frameColour":null,"baseColour":null,"headFootBoard":null,"mattressType":null,"productHeightLowest":null,"productHeightHighest":null,"liftingPoles":null,"grabRails":null,"readingLight":"0","existingBedHeight":null,"legHeight":null,"drawerOptions":null,"hoistCutouts":"0","cotSides":null,"liftingPole":"0","sideRetention":"0","linked":"0","rrp":null,"stock":"0"}]},{"0":{"ID":"756",""title":"Mr","name":"John","sur... 

我只是与这一个难住。或者我需要逐项列出数据集中的每个字段以输出干净的JSON?

+0

它可以帮助看到文本格式的原始输出,不是一些工具,成功地解释了它作为图像。 – Eiko

+0

当然。我会添加它。这纯粹是为了便于阅读。 – ABOO

回答

1

这不是从疑问清楚,但我想你需要的东西是这样的:

[ 
    { 
    "ID": 123, 
    ... 
    "products": [ 
     { 
     "foo": "bar" 
     }, 
     { 
     "foo": "baz" 
     } 
    ] 
    }, 
    { 
    ... 
    } 
] 

如果这是正确的,你需要重构你的代码一点点。首先,您必须将products数组放在$row_array数组中,该数组应该是$row,不包含它。由于$row似乎是公共属性的对象,你可以只投$row数组并将其分配给$row_array

$row_array = (array) $row; 

正如你所看到的,不需要一个$row_array用来包装$row,你$row_array应您的$row

最后,避免使用array_push()时,你必须只有一个元素在数组末尾推:

$json_response[] = $row_array; 

array_push()更快。

最后一块,你需要代码仅仅是这样的:

$orders = $db->get_results(("SELECT ords.serial as ID, cust.title, cust.name, cust.surname, cust.address, cust.address2, cust.town, cust.county, cust.postcode, cust.phone, cust.height, cust.weight, cust.houseType, cust.parking, cust.access, ords.furnitureRem, ords.furnitureRemDetails, ords.comments FROM orders as ords JOIN customers as cust ON ords.customerid = cust.serial JOIN order_detail as odeet ON ords.serial = odeet.orderid JOIN `user` as us ON us.id = ords.user_id WHERE us.id='".$_REQUEST['userID']."' GROUP BY ords.serial ORDER BY cust.serial DESC"); 

$json_response = array(); 
foreach ($orders as $row) { 
    $row_array = (array) $row; 
    $ord_id = $row->ID; 

    $orders2 = $db->get_results("SELECT * FROM order_detail as ord 
     JOIN products as prod ON ord.productid = prod.id 
     WHERE ord.orderid = ".$ord_id); 
    foreach ($orders2 as $vorder2) { 
     $row_array['products'][] = $vorder2; 
    } 
    $json_response[] = $row_array; 
} 
echo json_encode($json_response); 
+0

谢谢你的明确解释。现在它更有意义。我还没有看到“$ row_array =(array)$ row;”之前使用过。我喜欢! – ABOO

+0

因为'$ row_array'确实应该是'$ row',但'$ row'是一个对象,所以你可以用'(array)'来投射它。我会更新答案以使其更清楚。 – deshack