JSON data and php RecursiveArrayIterator

问题内容:

I have a JSON response from an API in the following format:

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}"

I need to get the question -> text,

id and choices

&& conditions with id, name and probability.

I tried the following:

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

No output. I also tried the php RecursiveArrayIterator as follows:

$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";
    }
}

and get error message that:

Passed variable is not an array or object

I am new to php and unable to understand the reason ?? Help solicited from experts.

Update:

var_dump(json_last_error(), json_last_error_msg());

gives int(4) string(12) “Syntax error”

Mailed the API provider regarding the issue and expecting reply.

问题评论:

    
check var_dump on $str on second line, I guess your file_get_contents is not reading your json file or the path is incorrect, is in the same directory where this file is running?
    
@pravindot17 I had checked var_dump earlier. It was working fine.
– Pamela
2 hours ago
    
@Pamela you get the NULL array as you have the error in JSON structure.
    
@Nazir Could you please spell out the error. It is coming from an API and i just copied it to a file.
– Pamela
1 hour ago
    
That’s not valid json up there.

答案:

答案1:

Hope this will help you, see here I am able access your json properties

<?php
$str = file_get_contents("https://api.myjson.com/bins/7gc8n");
$json = json_decode($str, true);

// checkout this I am getting json here
echo '<pre>';
// print_r($json, true);

print_r($json['question']['text']);
print_r($json['conditions']);
echo '</pre>';
?>

答案评论:

    
I am not getting any result ??
– Pamela
1 hour ago

答案2:

Try to copy this code in your test file and then get the result:

here is the error in JSON syntax:

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

Try bellow code sample

{
    "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
}

答案评论:

    
As informed the result is coming from an external API and i am testing it copying it to a file. Please note that changing the JSON is not the way to go. And using your JSON does not provide CHOICES.
– Pamela
1 hour ago
    
if we test original JSON code from your sample with decoding we get error Unexpected control character found
    
I did not get any such error. No Errors..No output ???
– Pamela
1 hour ago
    
try json_decode($str); then print json_last_error()); php.net/manual/en/function.json-last-error.php
    
var_dump($json, $error === JSON_ERROR_UTF8); gives NULL bool(false)
– Pamela
1 hour ago

原文地址:

https://stackoverflow.com/questions/47754592/json-data-and-php-recursivearrayiterator

Tags:,

添加评论

友情链接:蝴蝶教程