如何在 Retrofit 中获取 JSON 数组?
How to fetch JSON array in Retrofit?
我有一个 JSON 数组,里面有 JSON Objects
这样的:
[
{
"id": "63",
"userprofile": "userprofile.jpg",
"username": "James",
"content": "test.",
"address": "1111 Parker St",
"Post_date": "2022-05-25 02:41:15",
"images": [
"20220525_0241291.jpg",
"20220525_0241290.jpg"
]
}
]
我想将 images
数组中的字符串元素改为对象。谁能建议我如何制作这种 JSON?
[
{
"id": "63",
"userprofile": "userprofile.jpg",
"username": "James",
"content": "test.",
"address": "1111 Parker St",
"Post_date": "2022-05-25 02:41:15",
"images": [
{"image": "20220525_0241291.jpg"},
{"image": "20220525_0241290.jpg"}
]
}
]
这是我的 PHP 代码
<?php
header("Content-Type:application/json");
include '../db_con.php';
$query = "SELECT Board.* ,
GROUP_CONCAT(Board_images.imagepath separator ',') AS multiimages
FROM Board
LEFT JOIN Board_images ON Board.index = Board_images.boardindex
GROUP BY Board.index
ORDER BY Board.index";
$result = mysqli_query($conn, $query);
$response = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push(
$response,
array(
'id' => $row['index'],
'userprofile' => $row['userprofile'],
'username' => $row['username'],
'content' => $row['content'],
'address' => $row['address'],
'Post_date' => $row['Post_date'],
'images' => explode(',',$row['multiimages'])
)
);
}
echo json_encode($response);
?>
explode
returns 一个简单的字符串数组。您所要做的就是检查它并将每个字符串放入一个数组中。您可以循环执行此操作或使用 array function like array_map()
执行相同的操作,对每个项目应用回调。
您也不需要循环来填充您的响应数组,而是使用 mysqli_result::fetch_all()
. You could use a loop to add the extra images
element, but again I prefer using the array_walk()
function 作为个人意见。
(array_map
和array_walk
的区别在于array_map
returns根据回调returns生成的新数组。array_walk
仅修改现有数组。)
<?php
include '../db_con.php';
$query = "SELECT Board.* , GROUP_CONCAT(Board_images.imagepath separator ',') AS multiimages FROM Board
LEFT JOIN Board_images ON Board.index = Board_images.boardindex GROUP BY Board.index ORDER BY Board.index";
$result = $con->query($query);
// fetch all the records at once
$response = $result->fetch_all(\MYSQLI_ASSOC);
// this performs similar function to a foreach loop
array_walk(
$response,
function (&$v, $k) {
// explode the comma-separated list into an indexed array
$images = explode(",", $v["multiimages"]);
// now turn it into a bunch of associative arrays
$images = array_map(fn ($im) => ["image" => $im], $images);
// and then add it to the original $response array
$v["images"] = $images;
}
);
header("Content-Type:application/json");
echo json_encode($response);
我有一个 JSON 数组,里面有 JSON Objects
这样的:
[
{
"id": "63",
"userprofile": "userprofile.jpg",
"username": "James",
"content": "test.",
"address": "1111 Parker St",
"Post_date": "2022-05-25 02:41:15",
"images": [
"20220525_0241291.jpg",
"20220525_0241290.jpg"
]
}
]
我想将 images
数组中的字符串元素改为对象。谁能建议我如何制作这种 JSON?
[
{
"id": "63",
"userprofile": "userprofile.jpg",
"username": "James",
"content": "test.",
"address": "1111 Parker St",
"Post_date": "2022-05-25 02:41:15",
"images": [
{"image": "20220525_0241291.jpg"},
{"image": "20220525_0241290.jpg"}
]
}
]
这是我的 PHP 代码
<?php
header("Content-Type:application/json");
include '../db_con.php';
$query = "SELECT Board.* ,
GROUP_CONCAT(Board_images.imagepath separator ',') AS multiimages
FROM Board
LEFT JOIN Board_images ON Board.index = Board_images.boardindex
GROUP BY Board.index
ORDER BY Board.index";
$result = mysqli_query($conn, $query);
$response = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push(
$response,
array(
'id' => $row['index'],
'userprofile' => $row['userprofile'],
'username' => $row['username'],
'content' => $row['content'],
'address' => $row['address'],
'Post_date' => $row['Post_date'],
'images' => explode(',',$row['multiimages'])
)
);
}
echo json_encode($response);
?>
explode
returns 一个简单的字符串数组。您所要做的就是检查它并将每个字符串放入一个数组中。您可以循环执行此操作或使用 array function like array_map()
执行相同的操作,对每个项目应用回调。
您也不需要循环来填充您的响应数组,而是使用 mysqli_result::fetch_all()
. You could use a loop to add the extra images
element, but again I prefer using the array_walk()
function 作为个人意见。
(array_map
和array_walk
的区别在于array_map
returns根据回调returns生成的新数组。array_walk
仅修改现有数组。)
<?php
include '../db_con.php';
$query = "SELECT Board.* , GROUP_CONCAT(Board_images.imagepath separator ',') AS multiimages FROM Board
LEFT JOIN Board_images ON Board.index = Board_images.boardindex GROUP BY Board.index ORDER BY Board.index";
$result = $con->query($query);
// fetch all the records at once
$response = $result->fetch_all(\MYSQLI_ASSOC);
// this performs similar function to a foreach loop
array_walk(
$response,
function (&$v, $k) {
// explode the comma-separated list into an indexed array
$images = explode(",", $v["multiimages"]);
// now turn it into a bunch of associative arrays
$images = array_map(fn ($im) => ["image" => $im], $images);
// and then add it to the original $response array
$v["images"] = $images;
}
);
header("Content-Type:application/json");
echo json_encode($response);