从数据库创建 JSON 文件

creating json file from database

提问人:Ire4U 提问时间:6/11/2021 最后编辑:beerwinIre4U 更新时间:6/11/2021 访问量:82

问:

我需要按以下格式创建这个 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 数组 json mysqli

评论


答:

-1赞 Goutham 6/11/2021 #1

您可以尝试如下

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));


评论

0赞 Ire4U 6/11/2021
感谢您的回复。我应该说我也在使用 php。对不起。你用的是什么代码?
0赞 Goutham 6/11/2021
我在这里使用了 Javscript。您也可以在 php 中应用相同的方法。请参阅此文档 php.net/manual/en/function.array-reduce.php。看看你是否成功,否则我会在 php 中发布答案。
0赞 beerwin 6/11/2021 #2

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

// 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);

评论

0赞 Ire4U 6/11/2021
谢谢你。它让我走上了正确的轨道。唯一的问题是 orderId 和 orderDate 每次都在重复,我只想显示一次
0赞 beerwin 6/11/2021
我稍微更改了代码(将$message声明从行和循环之间移动到顶部)。我会调试bind_result部分,因为这就是您的问题所在。bind_paramswhile