【问题标题】:creating json file from database从数据库创建 json 文件
【发布时间】:2021-08-27 23:04:09
【问题描述】:

我需要按照以下格式创建这个json

{
  "status": true,
  "message": "",
  "orders": [
    {
      "orderId": "1",
      "orderDate": "1/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "5.00"
        },
        {
          "productName": "Product 2",
          "quantity": "2",
          "price": "24.00"
        },
        {
          "productName": "Product 3",
          "quantity": "1",
          "price": "6.50"
        }
      ]
    },
    {
      "orderId": "2",
      "orderDate": "2/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    },
    {
      "orderId": "3",
      "orderDate": "03/05/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    }
  ]
}

这是我用来从数据库中检索信息的代码

$stmt = $con->prepare("SELECT OrderID, OrderDate, ProductName, ProductQty, ProductPrice FROM Orders where CustomerID = ?");
$stmt->bind_param("s", $CustomerID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($OrderID, $OrderDate, $ProductName, $ProductQty, $ProductPrice); 
while($stmt->fetch()) { 
    $message[] = array(
        "status" => true, 
        "message" => "", 
        "orders" => array(
            array( 
                "orderId" => "$OrderID", 
                "orderDate" => "$OrderDate", 
                "products" => array(
                    array( 
                        "productName" => "$ProductName",
                        "quantity" => "$ProductQty", 
                        "price" => "$ProductPrice"
                    ), 
                )
            )
        )
    );
}

$json = $message;

header('content-type: application/json');
echo json_encode($json);

这就是来自数据库的信息的显示方式。我不知道如何正确显示有关产品的信息。谁能告诉我如何以我在 php 中需要的格式进行操作?提前感谢您的帮助。

[
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "620",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "240",
            "quantity": "1",
            "price": "5.00"
          },
          {
            "productName": "270",
            "quantity": "1",
            "price": "10.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "30",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "280",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "610",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  }
]

【问题讨论】:

    标签: php arrays json mysqli


    【解决方案1】:

    您正在重复您不应该重复的订单结构。这就是为什么你得到错误的结构。这是你应该做的(你的代码,修改):

    // specify your return response array only once
    $message = array(
        "status" => true,
        "message" => "",
        "orders" => array();
    );
    
    $stmt = $con->prepare("SELECT OrderID, OrderDate, ProductName, ProductQty, ProductPrice FROM Orders where CustomerID = ?");
    $stmt->bind_param("s", $CustomerID);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($OrderID, $OrderDate, $ProductName, $ProductQty, $ProductPrice); 
    
    while($stmt->fetch()) { 
        // and fill its "orders" member with your orders list
        $message["orders"][] = array(
            "orderId" => "$OrderID", 
            "orderDate" => "$OrderDate", 
            "products" => array(
                array( 
                    "productName" => "$ProductName",
                    "quantity" => "$ProductQty", 
                    "price" => "$ProductPrice"
                ), 
            )
        );
    }
    
    $json = $message;
    
    header('content-type: application/json');
    echo json_encode($json);
    

    【讨论】:

    • 谢谢。它让我开始走上正轨。唯一的问题是 orderId 和 orderDate 每次都重复,我只想显示一次
    • 我稍微更改了代码(将 $message 声明从bind_params 行和while 循环之间移到顶部)。我会调试 bind_result 部分,因为这就是你的问题所在。
    【解决方案2】:

    你可以像下面这样尝试

    const data = [
        {
            "status": true,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "620",
                            "quantity": "1",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": true,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "240",
                            "quantity": "1",
                            "price": "5.00"
                        },
                        {
                            "productName": "270",
                            "quantity": "1",
                            "price": "10.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": true,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "30",
                            "quantity": "1",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": true,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "280",
                            "quantity": "1",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": true,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "610",
                            "quantity": "1",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": false,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "800",
                            "quantity": "50",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        },
        {
            "status": false,
            "message": "",
            "orders": [
                {
                    "orderId": "1",
                    "orderDate": "1/06/2021",
                    "products": [
                        {
                            "productName": "1800",
                            "quantity": "50",
                            "price": "5.00"
                        }
                    ]
                }
            ]
        }
    ];
    
    const resutl = data.reduce((acc, cur) => {
        const key = cur.status ? "successItems" : "failureItems";
    
        if (acc[key]) {
            return {
                ...acc,
                [key]: {
                    status: cur.status,
                    messgae: '',
                    orders: [
                        ...acc[key].orders,
                        ...cur.orders
                    ]
                }
            }
        } else {
            return {
                ...acc,
                [key]: {
                    status: cur.status,
                    messgae: '',
                    orders: [
                        ...cur.orders
                    ]
                }
            }
        }
    }, {});
    console.log('resutl', resutl);
    
    console.log('resutl', Object.values(resutl));
    
    
    

    【讨论】:

    • 感谢您的回复。我应该说我也在使用 php。对于那个很抱歉。你用的是什么代码?
    • 我在这里使用了 Javscript。您也可以在 php 中应用相同的内容。请参阅此文档php.net/manual/en/function.array-reduce.php。看看你是否成功,否则我将在 php 中发布答案。
    最近更新 更多