这是json响应:
{
"level1": {
"level2":
{
"name": "abc",
"age": "19",
"email": "abc@g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}
}
}
我想按以下方式转换JSON响应:
{
"level1": {
"level2":
[{
"name": "abc",
"age": "19",
"email": "abc@g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}]
}
}
如果我在level2中有一个用户详细信息,我将XML响应转换为JSON形式并获取了对象,但我希望在level 2中的数组中甚至有多个用户详细信息或单个用户详细信息。.怎么可能? / p>
答案 0 :(得分:1)
请尝试使用注释中提到的 @simonecosci 这样的代码。
$responseData ='{
"level1": {
"level2":
{
"name": "abc",
"age": "19",
"email": "abc@g.com",
"gender": "Female",
"dob": "7 june 1999",
"add": "sdasdadas"
}
}
}';
$json = json_decode($responseData);
$json->level1->level2 = [$json->level1->level2];
echo '<pre>';
print_r(json_encode($json));
echo '</pre>';
预期输出:
{“ level1”:{“ level2”:[{“ name”:“ abc”,“ age”:“ 19”,“ email”:“ abc@g.com”,“ gender”:“女性” ,“ dob”:“ 7 1999年6月“,” add“:” sdasdadas“}]}}