为此JSON代码创建PHP部件的简单方法
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
]
}
我创建了简单的JSON响应但是多对象和数组存在问题。
答案 0 :(得分:1)
您可以创建如下数组:
Error.prepareStackTrace = function (err, stack) { return stack; };
currentfile = err.stack.shift().getFileName();
while (err.stack.length) {
callerfile = err.stack.shift().getFileName();
if(currentfile !== callerfile) return callerfile;
}
} catch (err) {}
return undefined;
结果:
$a = array("contacts" => array(
array(
"id" => "c200",
"name" => "Ravi Tamada",
"email" => "ravi@gmail.com",
"address" => "xx-xx-xxxx,x - street, x - country",
"gender" => "male",
"phone" => array(
"mobile" => "+91 0000000000",
"home" => "00 000000",
"office" => "00 000000"
)
),
array(
"id" => "c201",
"name" => "Johnny Depp",
"email" => "johnny_depp@gmail.com",
"address" => "xx-xx-xxxx,x - street, x - country",
"gender" => "male",
"phone" => array(
"mobile" => "+91 0000000000",
"home" => "00 000000",
"office" => "00 000000"
)
),
)
);
echo json_encode($a);
答案 1 :(得分:1)
这实际上很容易自己做
获取现有的JSON字符串,然后将其放入Map
。这将为您创建等效的PHP数据结构。
json_decode
答案 2 :(得分:0)
echo json_encode([
"contacts" => [
[
"id" => "c200",
"name" => "Ravi Tamada",
"email" => "ravi@gmail.com",
"address" => "xx-xx-xxxx,x - street, x - country",
"gender" => "male",
"phone" => [
"mobile" => "+91 0000000000",
"home" => "00 000000",
"office" => "00 000000"
]
],
[
"id" => "c201",
"name" => "Johnny Depp",
"email" => "johnny_depp@gmail.com",
"address" => "xx-xx-xxxx,x - street, x - country",
"gender" => "male",
"phone" => [
"mobile" => "+91 0000000000",
"home" => "00 000000",
"office" => "00 000000"
]
],
]
]);
答案 3 :(得分:0)
要创建一个从该(或另一个)json生成结构的PHP代码,您应该结合使用json_decode + var_export。
$json = '... your json code ...';
$phpcode = var_export(json_decode($json_string));
在您的特定情况下,它将为您的代码创建结构:
stdClass::__set_state(array(
'contacts' =>
array (
0 =>
stdClass::__set_state(array(
'id' => 'c200',
'name' => 'Ravi Tamada',
'email' => 'ravi@gmail.com',
'address' => 'xx-xx-xxxx,x - street, x - country',
'gender' => 'male',
'phone' =>
stdClass::__set_state(array(
'mobile' => '+91 0000000000',
'home' => '00 000000',
'office' => '00 000000',
)),
)),
1 =>
stdClass::__set_state(array(
'id' => 'c201',
'name' => 'Johnny Depp',
'email' => 'johnny_depp@gmail.com',
'address' => 'xx-xx-xxxx,x - street, x - country',
'gender' => 'male',
'phone' =>
stdClass::__set_state(array(
'mobile' => '+91 0000000000',
'home' => '00 000000',
'office' => '00 000000',
)),
)),
),
));
你仍然无法创建对象,因为stdClass没有方法__set_state,你可以在这里阅读更多相关内容:http://blog.thoughtlabs.com/blog/2008/02/02/phps-mystical-__set_state-method
注意:还有额外的","在你应该删除的第二个conctact元素之后。