在 php 中创建嵌套的 JSON 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15810257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create nested JSON object in php?
提问by Shane
I don't work with php much and I'm a little fuzzy on object creation. I need to make a webservice request sending json and I think I have that part covered. Before I can submit the data I need to create a nested object. I was assuming this would be trivial based on my experience with ecma based scripting languages, but I'm finding the syntax to be difficult to navigate. The object I want to create is below.
我不太会使用 php,而且我对对象创建有点模糊。我需要发出一个发送 json 的网络服务请求,我想我已经涵盖了这部分内容。在提交数据之前,我需要创建一个嵌套对象。根据我使用基于 ecma 的脚本语言的经验,我认为这很简单,但我发现语法很难导航。我要创建的对象如下。
{ "client": {
"build": "1.0",
"name": "xxxxxx",
"version": "1.0"
},
"protocolVersion": 4,
"data": {
"distributorId": "xxxx",
"distributorPin": "xxxx",
"locale": "en-US"
}
}
I've seen a lot of examples of flat objects, but I haven't found a minimal example for a nested object yet. What would be the php syntax for the object above? Is this an unusual thing to do in php?
我看过很多平面对象的例子,但我还没有找到嵌套对象的最小例子。上面对象的 php 语法是什么?这是在 php 中做的不寻常的事情吗?
回答by Ejaz
this JSON structure can be created by following PHP code
这个 JSON 结构可以通过以下 PHP 代码创建
$json = json_encode(array(
"client" => array(
"build" => "1.0",
"name" => "xxxxxx",
"version" => "1.0"
),
"protocolVersion" => 4,
"data" => array(
"distributorId" => "xxxx",
"distributorPin" => "xxxx",
"locale" => "en-US"
)
));
see json_encode
回答by Guillermo Tallano
Hey here is a quick trick to manually convert complex JSONs into a PHP Object.
嘿,这是一个手动将复杂的 JSON 转换为 PHP 对象的快速技巧。
Grab the JSON example as you have:
获取 JSON 示例:
{ "client": {
"build": "1.0",
"name": "xxxxxx",
"version": "1.0"
},
"protocolVersion": 4,
"data": {
"distributorId": "xxxx",
"distributorPin": "xxxx",
"locale": "en-US"
}
}
Search-Replace {
to array(
搜索替换 {
到array(
Search-Replace :
to =>
搜索替换 :
到=>
Search-Replace }
to )
搜索替换 }
到)
Done.
完毕。
回答by Devesh
User array to get the correct format and then call echo json_encode(array)
用户数组获取正确格式然后调用echo json_encode(array)
array( "client" => array(
"build" => "1.0",
"name" => "xxxxxx",
"version" => "1.0"
),
"protocolVersion" => 4,
"data" => array(
"distributorId" => "xxxx",
"distributorPin" => "xxxx",
"locale" => "en-US"
))
回答by ilikemypizza
$client = new Client();
$client->information = new Information();
$client->information->build = '1.0';
$client->information->name = 'xxxxxx';
$client->information->version = '1.0';
$client->protocolVersion = 4;
$client->data = new Data();
$client->data->distributorId = "xxxx";
$client->data->distributorPin = "xxxx";
$client->data->locale = "en-US";
Perhaps something like the above? The client would hold two objects. Information and Data.
也许像上面那样?客户端将持有两个对象。信息和数据。
EditUsing json_encode, you would create this object as an array in PHP..
编辑使用 json_encode,您可以在 PHP 中将此对象创建为数组。
$clientObj = array('client'=>
array( array('build'=>'1.0','name'=>'xxxx', 'version'=>'1.0'),
'protocolVersion'=>4,
'data'=>array('distributorId' => 'xxxx', 'distributorPin' => 'xxxx', 'locale' => 'en-US')
);
print json_encode($clientObj);
回答by MSVKC
We can also construct nested array and then do a json_encode to construct nested JSON.
我们也可以构造嵌套数组,然后做一个 json_encode 来构造嵌套的 JSON。
For e.g:
例如:
{"User":
{"username":"test",
"address":"Posted value fro address field",
"location":{
"id":12345
}
}
}
Above output we can achieve by writing below php code:
以上输出我们可以通过编写下面的php代码来实现:
<?php
$obj = array(
'username'=>$lv_username,
'address'=>$lv_address,
'location'=>array('id'=>$lv_locationId)
);
$data = '{"User":'. json_encode($obj) .'}';
echo $data;
?>
Hope it helps.
希望能帮助到你。
回答by Marty
You can use json_encode to encode a php array http://php.net/manual/en/function.json-encode.php
您可以使用 json_encode 对 php 数组进行编码 http://php.net/manual/en/function.json-encode.php
$theArray = array('client'= array('build'=>'1.0',
'name'=>'xxxxx',
'version'=>'1.0'
),
'protocolVersion'=> 4,
'data'=> array('distributorId'=>'xxxx',
'distributorPin'=>'xxxx',
'locale'=>'en-US'
)
);
$theObj = json_encode($theArray);
hopefully this helps..
希望这有帮助..
posted it, then seen loads of answers already! :|
发布它,然后已经看到了大量的答案!:|
回答by Code L?ver
Use the in build function of PHP:
使用PHP的in build函数:
this will convert the array into JSON object.
这会将数组转换为 JSON 对象。