使用php提取每个级别的数据
下面是JSon数据使用php提取每个级别的数据,php,arrays,json,Php,Arrays,Json,下面是JSon数据 { "statusType": "OK", "entity": [ { "category": "category1","difficultyLevel": "Easy", "quizAnswerChoices": [{"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd254
{
"statusType": "OK",
"entity": [
{
"category": "category1","difficultyLevel": "Easy",
"quizAnswerChoices": [{"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}, {"choiceText": "Yes", "choiceTextHash": "c3f1130841b507a4d1e0f45971d990c6ecd25406"}]
}
],
"entityType": "java.util.ArrayList",
"status": 200,
"metadata": {}
}
我需要解析
-实体
-quizAnswerChoices(清点物品)
如果您执行类似json的解码,如何检索每个
选项text
etc
$data = json_decode(json_data);
那么$data将是一个可遍历的数组
$data['entity']['quizAnswerChoices'] etc
让我们考虑在变量名为<代码>“$内容”< /COD>中接收此JSON。 你所要做的就是解码它:
$decoded = json_decode($contents, true); //True so, the decoded object will be converted into an ssociative array
print_r($decoded);
使用
用这个
$arry = json_decode($json,true);
foreach ($arry['entity'] as $ke => $ve) {
foreach ($ve['quizAnswerChoices'] as $k => $v) {
print_r($v);
}
}
使用
json\u decode
将json转换为数组
$dataArray = json_decode($json_string, true)
$dataArray['entity']; // entity
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
count($dataArray['entity']['quizAnswerChoices']); // quizAnswerChoices count
向我们展示您如何将json数据转换为PHP数组,然后从那里开始。例如:
count($arrayname['entity']['quizAnswerChoices'])
。
$dataArray = json_decode($json_string, true)
$dataArray['entity']; // entity
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
$dataArray['entity']['quizAnswerChoices']; // quizAnswerChoices
count($dataArray['entity']['quizAnswerChoices']); // quizAnswerChoices count