2016-07-26 53 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