php - JSON 数据和 php RecursiveArrayIterator

标签 php json

我有一个来自 API 的 JSON 响应,格式如下:

string(2228) "{"question":{"type":"single","text":"Have you noticed any insects on you or have you been bitten by any insect (lice, mosquitoes, ticks, bedbugs, etc.)?",
"items":
[
{"id":"p_48","name":"Insect bite","
choices":
[{"id":"present","label":"Yes"},
{"id":"absent","label":"No"},
{"id":"unknown","label":"Don't know"}
]}
],
"extras":{}
},
"conditions":[
{"id":"c_87","name":"Common cold","common_name":"Common cold","probability":0.028},
{"id":"c_10","name":"Gastroenteritis","common_name":"Gastroenteritis","probability":0.0238},
{"id":"c_49","name":"Migraine","common_name":"Migraine","probability":0.0096}
],
"extras":{},"should_stop":false}"

我需要获取问题 -> 文本

id 和选择

&& 带有id、名称和概率的条件

我尝试了以下方法:

<?php
$str = file_get_contents("test.json");
//var_dump($str);
$json = json_decode($str, true);
echo '<pre>' . print_r($json, true) . '</pre>';
?>

没有输出。我还尝试了 php RecursiveArrayIterator,如下所示:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

并得到错误信息:

Passed variable is not an array or object

我是 php 新手,无法理解原因??向专家寻求帮助。

更新:

我直接从终端调用 CURL,发现它缺少 string(2228) " 和结尾的 " 部分。所以我确实爆炸了。因为它是一个多维数组,所以我做了递归数组迭代器,如下所示:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($str, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

转到所需的输出。问题已解决。

最佳答案

尝试将此代码复制到您的测试文件中,然后得到结果:

这里是 JSON 语法错误:

"name": "Insect bite",
            "
            choices "

试试下面的代码示例

{
    "question": {
        "type": "single",
        "text": "Have you noticed any insects on you or have you been bitten by any insect (lice, mosquitoes, ticks, bedbugs, etc.)?",
        "items": [{
            "id": "p_48",
            "name": "Insect bite",
            "choices": [{
                    "id": "present",
                    "label": "Yes"
                },
                {
                    "id": "absent",
                    "label": "No"
                },
                {
                    "id": "unknown",
                    "label": "Don't know"
                }
            ]
        }],
        "extras": {}
    },
    "conditions": [{
            "id": "c_87",
            "name": "Common cold",
            "common_name": "Common cold",
            "probability": 0.028
        },
        {
            "id": "c_10",
            "name": "Gastroenteritis",
            "common_name": "Gastroenteritis",
            "probability": 0.0238
        },
        {
            "id": "c_49",
            "name": "Migraine",
            "common_name": "Migraine",
            "probability": 0.0096
        }
    ],
    "extras": {},
    "should_stop": false
}

关于php - JSON 数据和 php RecursiveArrayIterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754592/

相关文章:

javascript - 如何缩进二维数组?

java - 使用 Gson 创建大型 Json 字符串的正确方法

java - json官方java api : JSONArray of JSONObjects

PHP/Laravel - 有条件地显示 Blade View

php - 我的 MySQL 查询有什么问题?

php - 页面不会重定向,因为没有插入数据

php - MySQL 选择性显示状态

c# - 中继 JSON 已转义双引号

php - 从 laravel 中的数据库动态填充语言翻译数组

json - JScript JSON 对象检查