php php中的json解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1270016/
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
json decode in php
提问by pgtips
I have the following json string and I want to retrieve just the email address from it. How do I do it in php?
我有以下 json 字符串,我只想从中检索电子邮件地址。我如何在 php 中做到这一点?
{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}}
回答by Pascal MARTIN
Considering you have json_decoded you data this way :
考虑到你有json_decode这样的数据:
$data = json_decode($json);
You can use var_dump(well, it's output looks way better if used with the Xdebugextension, which is nice to have on a development machine, btw)to know what's in your data :
您可以使用(好吧,如果与Xdebug扩展一起使用,它的输出看起来会更好,顺便说一句,这在开发机器上很好用)来了解您的数据:var_dump
// Allows you to know what's in the data ;-)
var_dump($data);
You'll get something like this :
你会得到这样的东西:
object(stdClass)[1]
public 'communications' =>
object(stdClass)[2]
public 'communication' =>
array
0 =>
object(stdClass)[3]
public '@array' => string 'true' (length=4)
public '@id' => string '23101384' (length=8)
public '@uri' => string 'xyz/v1/Communications/1111' (length=26)
public 'household' =>
object(stdClass)[4]
public '@id' => string '111111' (length=6)
public '@uri' => string 'xyz/v1/Households/5465465' (length=25)
public 'person' =>
object(stdClass)[5]
public '@id' => string '' (length=0)
public '@uri' => string '' (length=0)
public 'communicationType' =>
object(stdClass)[6]
public '@id' => string '1' (length=1)
public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/1' (length=42)
public 'name' => string 'Home Phone' (length=10)
public 'communicationGeneralType' => string 'Telephone' (length=9)
public 'communicationValue' => string '1111' (length=4)
public 'searchCommunicationValue' => string '2693240758' (length=10)
public 'listed' => string 'true' (length=4)
public 'communicationComment' => null
public 'createdDate' => string '2008-11-10T12:31:26' (length=19)
public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19)
1 =>
object(stdClass)[7]
public '@array' => string 'true' (length=4)
public '@id' => string '11111' (length=5)
public '@uri' => string 'xyz/v1/Communications/111111111' (length=31)
public 'household' =>
object(stdClass)[8]
public '@id' => string '14436295' (length=8)
public '@uri' => string 'xyz/v1/Households/11111' (length=23)
public 'person' =>
object(stdClass)[9]
public '@id' => string '2222222' (length=7)
public '@uri' => string 'xyz/v1/People/22222222' (length=22)
public 'communicationType' =>
object(stdClass)[10]
public '@id' => string '2' (length=1)
public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/2' (length=42)
public 'name' => string 'Work Phone' (length=10)
public 'communicationGeneralType' => string 'Telephone' (length=9)
public 'communicationValue' => string '11111' (length=5)
public 'searchCommunicationValue' => string '789787987' (length=9)
public 'listed' => string 'false' (length=5)
public 'communicationComment' => null
public 'createdDate' => string '2009-08-09T15:49:27' (length=19)
public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19)
2 =>
object(stdClass)[11]
public '@array' => string 'true' (length=4)
public '@id' => string '11111' (length=5)
public '@uri' => string 'xyz/v1/Communications/11111' (length=27)
public 'household' =>
object(stdClass)[12]
public '@id' => string '1111' (length=4)
public '@uri' => string 'xyz/v1/Households/1111' (length=22)
public 'person' =>
object(stdClass)[13]
public '@id' => string '244404' (length=6)
public '@uri' => string 'xyz/v1/People/1111' (length=18)
public 'communicationType' =>
object(stdClass)[14]
public '@id' => string '3' (length=1)
public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/3' (length=42)
public 'name' => string 'Mobile' (length=6)
public 'communicationGeneralType' => string 'Telephone' (length=9)
public 'communicationValue' => string '22222' (length=5)
public 'searchCommunicationValue' => string '5475454' (length=7)
public 'listed' => string 'true' (length=4)
public 'communicationComment' => null
public 'createdDate' => string '2008-11-10T12:31:26' (length=19)
public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19)
3 =>
object(stdClass)[15]
public '@array' => string 'true' (length=4)
public '@id' => string '15454' (length=5)
public '@uri' => string 'xyz/v1/Communications/111111' (length=28)
public 'household' =>
object(stdClass)[16]
public '@id' => string '14436295' (length=8)
public '@uri' => string 'xyz/v1/Households/1111' (length=22)
public 'person' =>
object(stdClass)[17]
public '@id' => string '244444474' (length=9)
public '@uri' => string 'xyz/v1/People/111111' (length=20)
public 'communicationType' =>
object(stdClass)[18]
public '@id' => string '4' (length=1)
public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/4' (length=42)
public 'name' => string 'Email' (length=5)
public 'communicationGeneralType' => string 'Email' (length=5)
public 'communicationValue' => string '[email protected]' (length=18)
public 'searchCommunicationValue' => string '[email protected]' (length=18)
public 'listed' => string 'true' (length=4)
public 'communicationComment' => null
public 'createdDate' => string '2008-11-10T12:31:26' (length=19)
public 'lastUpdatedDate' => string '2009-08-11T23:39:06' (length=19)
Which means you should be able to access the data you're looking for with something like this :
这意味着您应该能够使用以下内容访问您正在寻找的数据:
foreach ($data->communications->communication as $communication) {
if ($communication->communicationGeneralType == 'Email') {
var_dump($communication->communicationValue);
var_dump($communication->searchCommunicationValue);
}
}
Which will get you :
这会让你:
string '[email protected]' (length=18)
string '[email protected]' (length=18)
"communications" is an object, that contains "communication", which is an array of objects, each one containing a communicationGeneralType, which allows you to determine whether the current communication is an EMail or not.
“ communications”是一个对象,其中包含“ communication”,这是一个对象数组,每个对象都包含一个communicationGeneralType,它允许您确定当前通信是否为电子邮件。
WHen it is, you can use the communicationValueor searchCommunicationValuefields.
如果是,您可以使用communicationValue或searchCommunicationValue字段。
And I don't really see a way of doing this without iterating over every communicationelement...
而且我真的没有看到一种方法可以在不迭代每个communication元素的情况下做到这一点......
Hope this helps!
希望这可以帮助!
回答by Pascal MARTIN
Another twist on how inerte did it would be to access it like:
关于惰性如何做到的另一个转折是访问它,例如:
$json_object = '{"communications":
{"communication":
[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":
{"@id":"111111","@uri":"xyz/v1/Households/5465465"},
"person": {"@id":"","@uri":""},
"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},
"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758",
"listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},
{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":
{"@id":"14436295","@uri":"xyz/v1/Households/11111"},
"person": {"@id":"2222222","@uri":"xyz/v1/People/22222222"},
"communicationType": {"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},
"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false",
"communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},
{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household": {"@id":"1111","@uri":"xyz/v1/Households/1111"},
"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},
"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},
"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true",
"communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},
{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},
"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},
"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},
"communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true",
"communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}
]
}
}';
$json_decoded = json_decode($json_object);
echo "email: ".$json_decoded->communications->communication[3]->communicationValue."<br />";
回答by nickf
You could use json_decode(). Your example string there is a bit complicated for me to think about right now, but as an example of my own:
你可以使用json_decode(). 您的示例字符串现在对我来说有点复杂,但作为我自己的示例:
$json = '{"a":"apples","b":["bananas","boysenberries"],"c":"carrots"}';
$arr = json_decode($json);
echo $arr['a']; // "apples"
echo $arr['b'][0]; // "bananas"
回答by inerte
<?php
$string = '{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}}';
$encoded = json_decode($string, JSON_FORCE_OBJECT);
echo $encoded['communications']['communication'][3]['communicationValue'];
echo "\n";
echo $encoded['communications']['communication'][3]['searchCommunicationValue'];
?>
But if communicationValueor searchCommunicationValueever get out of index 3 of communication, you're in trouble. You'll probably have to loop communicationand search for these strings on its keys/values.
但是,如果communicationValue或searchCommunicationValue曾经超出 的索引 3 communication,您就会遇到麻烦。您可能必须communication在其键/值上循环和搜索这些字符串。
回答by social pilgrim
Nice example written by me
我写的很好的例子
Pass the json_object and 1 in function, you will see all values as per structure.
在函数中传递 json_object 和 1,您将看到每个结构的所有值。
function decodejson($value,$num){
if (count($value,0) > 0 && is_array($value)){
foreach ($value as $key =>$tvalue){
if (is_array($tvalue)){
//echo $key."-<br />";
$num++;
decodejson($tvalue,$num);
}else
echo str_repeat(" ", $num).$key."->".$tvalue."<br />";
}
}else
echo str_repeat(" ", $num).$key."->".$value."<br />";
}

