使用php构建json响应
我试图处理具有复杂结构的JSON响应,并且我不知道如何在多次尝试后对其进行管理。使用php构建json响应
JSON响应必须是这样的:
"note": {
"vote": 3,
"items":[
{
"value": 1,
"touchArea": {
"x": 122,
"y": 173,
"w": 89,
"h": 89
}
},
{
"value": 2,
"touchArea": {
"x": 122,
"y": 283,
"w": 89,
"h": 89
}
},
{
"value": 3,
"touchArea": {
"x": 122,
"y": 390,
"w": 89,
"h": 89
}
}
]
注:“投票”是阵列
As源的最大值,我要求MYSQL和我得到这个阵列($触摸) :
Array ([0] => V:1,X:122,Y:173,W:89,H:89 [1] => V:2,X:122,Y:283,W:89,H:89 [2] => V:3,X:122,Y:390,W:89,H:89)
我的问题是:如何从PHP生成这个带有循环的JSON响应,在这个例子中我们只有3个值,但它可能更多。
您可以使用以下命令编辑并重新保存json文件或字符串中的数据。
<?php
$sInFile = 'in2.json';
$sOutFile = 'out2.json';
$aDevelopers = array();
$aArtists = array();
$aCooks = array();
$sRaw = file_get_contents($sInFile);
var_dump($sRaw);
// object
$oData = json_decode($sRaw);
//array
$aData = json_decode($sRaw, 1);
$aNote = $aData[ 'note' ];
$aItems = $aNote[ 'items' ];
var_dump($aData);
$iCountData = count($aItems);
// Loops through items and set new values.
for($i = 0; $i < $iCountData; ++$i)
{
$aItem = $aItems[ $i ];
$aItem[ 'value' ] = 'newvalue';
$aItem[ 'touchArea' ][ 'x' ] = 3455;
$aItem[ 'touchArea' ][ 'y' ] = 43;
$aItems[ $i ] = $aItem;
}
$aNote[ 'items' ] = $aItems;
$aData[ 'note' ] = $aNote;
var_dump($aData);
$sNewJson = json_encode($aData);
file_put_contents($sOutFile, $sNewJson);
?>
假设你的结果从MySQL设置为一个字符串数组,如你的问题暗示,你需要遍历这个结果集,分割字符串上,
,然后:
,按摩前数据转换成你想要的结构,然后再转换成json。
下面应该为你的工作给你提供的信息:
<?php
// your initial result set from mysql - an array of strings
$touch = [
'V:1,X:122,Y:173,W:89,H:89',
'V:2,X:122,Y:283,W:89,H:89',
'V:3,X:122,Y:390,W:89,H:89',
];
// map over each element in your result set...
$items = array_map(function($item) {
$array = [];
// first, break each string on `,` to
// get an array of elements; e.g:
// ['V:1', 'X:122', 'Y:173', 'W:89', 'H:89'] etc.
$itemElements = explode(',', $item);
// then, iterate over these elements...
foreach ($itemElements as $itemElement) {
// explode on `:` and assign as a key value pair
list($key, $value) = explode(':', $itemElement);
// then, check the key and add the
// value to the array as appropriate
if ('V' === $key) {
$array['value'] = $value;
} else {
$array['touchArea'][strtolower($key)] = $value;
}
}
return $array;
}, $touch);
// then, build the `$note` array...
$note = [
'note' => [
'vote' => count($items),
'items' => $items,
]
];
// finally, json encode
echo json_encode($note, JSON_PRETTY_PRINT);
这产生了:
{
"note": {
"vote": 3,
"items": [
{
"value": "1",
"touchArea": {
"x": "122",
"y": "173",
"w": "89",
"h": "89"
}
},
{
"value": "2",
"touchArea": {
"x": "122",
"y": "283",
"w": "89",
"h": "89"
}
},
{
"value": "3",
"touchArea": {
"x": "122",
"y": "390",
"w": "89",
"h": "89"
}
}
]
}
}
希望这有助于:)
解决方案与array_map
,explode
,array_column
和max
功能:
$touch = ["V:1,X:122,Y:173,W:89,H:89", "V:2,X:122,Y:283,W:89,H:89", "V:3,X:122,Y:390,W:89,H:89"];
$result_arr = ["note" => ["vote" => 0, "items" => []]]; // basic structure
array_map(function($v) use (&$result_arr){
$arr = explode(",", $v);
$result = [];
foreach ($arr as $value) {
$new_arr = explode(":", $value);
$key = strtolower($new_arr[0]);
if ($key == "v"){
$result["value"] = $new_arr[1];
} else {
$result["touchArea"][$key] = $new_arr[1];
}
}
$result_arr["note"]["items"][] = $result;
}, $touch);
// getting max vote
$result_arr["note"]["vote"] = max(array_column($result_arr["note"]["items"], "value"));
$final_json = json_encode($result_arr, JSON_PRETTY_PRINT);
Thx寻求帮助。这将适用于PHP5?原因ie $ result_arr = [“note”=> [“vote”=> 0,“items”=> []]];对于PHP5 – Rodolphe
无效,您已经忘记了该版本。这段代码通常适用于PHP5。从PHP 5.4开始,您还可以使用短阵列语法。从PHP 5.5开始,你可以使用很好的函数'array_column'。今天升级并“将风帆修剪”。 (我们已经有PHP 7.0.4)(我的代码没有任何问题) – RomanPerekhrest
Thx。它绝对有帮助! – Rodolphe
每个元素都是一个字符串;例如:'V:1,X:122,Y:173,W:89,H:89'? –