在PHP中解析Json文件

时间:2014-04-11 17:24:45

标签: php json

我想解析php函数中的json文件,以查找status是否等于" created"是不是和那个,但是:

function getRsponseFromJSON ($json){
$status = "status";
$chaine = json_decode($json);
    foreach($chaine->status as $stat) {
        if ($stat == "created") {
            return $resp = "ok";
        } else {
            return $resp = "notok";
    }
}
}

这是我的json文件:

{
"data": {
    "aliases": [],
    "app_url": "http://myapp-MyDomain.rhcloud.com/",
    "build_job_url": null,
    "building_app": null,
    "building_with": null,
    "creation_time": "2013-04-22T03:12:13Z",
    "domain_id": "MyDomain",
    "embedded": {
        "haproxy-1.4": {}
    },
"status": "created",
"type": "application",
}

以及我得到的错误:

Warning: Invalid argument supplied for foreach() 

1 个答案:

答案 0 :(得分:3)

$chaine->status是一个字符串,所以我想你只需放弃这个无用的foreach,就像这样:

<?php
function getRsponseFromJSON ($json){
    $chaine = json_decode($json);

    return ($chaine->status == "created") ? "ok": "notok";
}