如何格式化 PHP 中 JSON 对象的 Key/Meta 数据
How to Format Key/Meta data of the JSON Object in PHP
给定以下代码片段:
<?php
require "db_conn.php";
$sql = "SELECT category,product_code,product_name,unit,quantity FROM items INNER JOIN categories ON items.category_id=categories.id";
$res = $conn->query($sql)->fetchAll();
$structured = [];
if ($res) {
foreach ($res as $key => $row)
{
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
echo json_encode([$structured]);
}
输出以下 JSON 响应:
[{
"BABY ITEMS": {
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
"CONFECTIONS\/PASTRIES": {
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
}, {
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}]
},
"HOUSEHOLD ITEMS": {
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
}
}]
如何修改这部分代码:
$structured[$row['category']]['items'][]
输出:
"category": "BABY ITEMS",
而不是:
"BABY ITEMS":
预期的JSON是(手写):
[{
"category": "BABY ITEMS",
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
{
"category": "CONFECTIONS/PASTRIES",
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "3.00"
},
{
"product_code": "115160",
"product_name": "NUVITA MAGIK S.BERRY CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}
]
},
{
"category": "HOUSEHOLD ITEMS",
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
},
{
"category": "PERSONAL CARE",
"items": [{
"product_code": "160200",
"product_name": "ALL TYME ULTRA REGULAR 8'S",
"unit": "CARTON",
"quantity": "2.00"
},
{
"product_code": "160309",
"product_name": "ALL TYME ULTRA MEDIUM 8'S",
"unit": "CARTON",
"quantity": "4.00"
},
{
"product_code": "160235",
"product_name": "GOLDEN SHOE POLSIH 50ML BLACK",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "190251",
"product_name": "ALL TYME ULTRA MEDIUM 16'S",
"unit": "CARTON",
"quantity": "2.00"
}]
}
]
我真的需要 JSON 格式如上,因为我正在使用它来创建嵌套类别和项目 RecyclerView。这就是 android java:
中的解析方式
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objCategory = jsonArray.getJSONObject(i);
System.out.println("Category: " + objCategory.getString("category"));
// fetch item details and store it in arraylists
JSONArray itemArray = objCategory.getJSONArray("items");
//System.out.println("Item length: " + itemArray.length());
ParentItem category = new
ParentItem(objCategory.getString("category"),OrderItemsList(itemArray));
orderFetchingList.add(category);
}
请看下面的代码(功能齐全)。我用很少的记录嘲笑了 $res
并复制了你的代码来填充 $structured
。在此之下,请找到我的代码来重组数据以获得所需的 JSON 输出
$res = [
[
'category' => 'BABY',
'product_code' => '123',
'product_name' => 'BABY STUFF',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'BABY',
'product_code' => '125',
'product_name' => 'BABY EAT',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'MEN',
'product_code' => '124',
'product_name' => 'MEN STUFF',
'unit' => 2,
'quantity' => 888,
]
];
echo '<pre>';
$structured = [];
foreach($res as $key => $row) {
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
print_r($structured);
// restructure the data to get the desired JSON output
$collection = [];
foreach($structured as $category => $items) {
$collection[] = [
'category' => $category,
'items' => $items['items']
];
}
echo json_encode($collection);
输出(JSON):
[
{
"category": "BABY",
"items": [
{
"product_code": "123",
"product_name": "BABY STUFF",
"unit": 1,
"quantity": 999
},
{
"product_code": "125",
"product_name": "BABY EAT",
"unit": 1,
"quantity": 999
}
]
},
{
"category": "MEN",
"items": [
{
"product_code": "124",
"product_name": "MEN STUFF",
"unit": 2,
"quantity": 888
}
]
}
]
给定以下代码片段:
<?php
require "db_conn.php";
$sql = "SELECT category,product_code,product_name,unit,quantity FROM items INNER JOIN categories ON items.category_id=categories.id";
$res = $conn->query($sql)->fetchAll();
$structured = [];
if ($res) {
foreach ($res as $key => $row)
{
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
echo json_encode([$structured]);
}
输出以下 JSON 响应:
[{
"BABY ITEMS": {
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
"CONFECTIONS\/PASTRIES": {
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
}, {
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}]
},
"HOUSEHOLD ITEMS": {
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
}
}]
如何修改这部分代码:
$structured[$row['category']]['items'][]
输出:
"category": "BABY ITEMS",
而不是:
"BABY ITEMS":
预期的JSON是(手写):
[{
"category": "BABY ITEMS",
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
{
"category": "CONFECTIONS/PASTRIES",
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "3.00"
},
{
"product_code": "115160",
"product_name": "NUVITA MAGIK S.BERRY CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}
]
},
{
"category": "HOUSEHOLD ITEMS",
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
},
{
"category": "PERSONAL CARE",
"items": [{
"product_code": "160200",
"product_name": "ALL TYME ULTRA REGULAR 8'S",
"unit": "CARTON",
"quantity": "2.00"
},
{
"product_code": "160309",
"product_name": "ALL TYME ULTRA MEDIUM 8'S",
"unit": "CARTON",
"quantity": "4.00"
},
{
"product_code": "160235",
"product_name": "GOLDEN SHOE POLSIH 50ML BLACK",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "190251",
"product_name": "ALL TYME ULTRA MEDIUM 16'S",
"unit": "CARTON",
"quantity": "2.00"
}]
}
]
我真的需要 JSON 格式如上,因为我正在使用它来创建嵌套类别和项目 RecyclerView。这就是 android java:
中的解析方式 for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objCategory = jsonArray.getJSONObject(i);
System.out.println("Category: " + objCategory.getString("category"));
// fetch item details and store it in arraylists
JSONArray itemArray = objCategory.getJSONArray("items");
//System.out.println("Item length: " + itemArray.length());
ParentItem category = new
ParentItem(objCategory.getString("category"),OrderItemsList(itemArray));
orderFetchingList.add(category);
}
请看下面的代码(功能齐全)。我用很少的记录嘲笑了 $res
并复制了你的代码来填充 $structured
。在此之下,请找到我的代码来重组数据以获得所需的 JSON 输出
$res = [
[
'category' => 'BABY',
'product_code' => '123',
'product_name' => 'BABY STUFF',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'BABY',
'product_code' => '125',
'product_name' => 'BABY EAT',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'MEN',
'product_code' => '124',
'product_name' => 'MEN STUFF',
'unit' => 2,
'quantity' => 888,
]
];
echo '<pre>';
$structured = [];
foreach($res as $key => $row) {
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
print_r($structured);
// restructure the data to get the desired JSON output
$collection = [];
foreach($structured as $category => $items) {
$collection[] = [
'category' => $category,
'items' => $items['items']
];
}
echo json_encode($collection);
输出(JSON):
[
{
"category": "BABY",
"items": [
{
"product_code": "123",
"product_name": "BABY STUFF",
"unit": 1,
"quantity": 999
},
{
"product_code": "125",
"product_name": "BABY EAT",
"unit": 1,
"quantity": 999
}
]
},
{
"category": "MEN",
"items": [
{
"product_code": "124",
"product_name": "MEN STUFF",
"unit": 2,
"quantity": 888
}
]
}
]