如何在php中解码json String

时间:2015-08-20 14:42:24

标签: php json curl

这是Json字符串

{"Header":{},
"Body":
    {"ResponseMsg":
        {"response":
            {"ResponseCode":"0020",
                "ResponseDesc": "user is not accepted!",
                "ConversationID": {},
                "OriginatorConversationID":{},
                "ServiceStatus":"0"
            }
        }
     }
}

这是我用来解码此字符串的代码;

   $dec = json_decode($json,true);
   var_dump($result->Header->Body->response->ResponseCode);

但我继续收到以下错误

  

注意:在 .... C C:\ xampp \ htdocs \ web \ server.php 中尝试获取非对象的属性/ b>

请协助解码这个格式错误的json字符串

2 个答案:

答案 0 :(得分:1)

标题正文处于同一级别。 试试这个:

var_dump($result["Body"]["response"]["ResponseCode"]);

答案 1 :(得分:0)

试试这个。

$dec = json_decode('{"Header":{},
"Body":
{"ResponseMsg":
    {"response":
        {"ResponseCode":"0020",
            "ResponseDesc": "user is not accepted!",
            "ConversationID": {},
            "OriginatorConversationID":{},
            "ServiceStatus":"0"
        }
    }
 }
}');

echo $dec->Body->ResponseMsg->response->ResponseCode;

Result: '0020'

您不需要使用标题,因为它与身体处于同一水平。