用PHP解析json。获取未定义的属性:stdClass错误
我在使用PHP解析来自Jenkins的json文件时遇到一些问题用PHP解析json。获取未定义的属性:stdClass错误,php,json,Php,Json,我在使用PHP解析来自Jenkins的json文件时遇到一些问题 { "actions" : [ { "causes" : [ { "shortDescription" : "Started by an SCM change" } ] }, { }, { }, { "buildsByBranchName" : { "ori
{
"actions" : [
{
"causes" : [
{
"shortDescription" : "Started by an SCM change"
}
]
},
{
},
{
},
{
"buildsByBranchName" : {
"origin/release_5.6.0" : {
"buildNumber" : 242,
"buildResult" : null,
"marked" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
},
"revision" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
}
},
"origin/release_5.7.0" : {
"buildNumber" : 315,
"buildResult" : null,
"marked" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
},
"revision" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
}
},
我试着做了以下几件事
//Read in JSON object
$json_file2 = file_get_contents('url.com/json');
//Decode JSON file
$test = json_decode($json_file2); //object
//print_r($json_file2);
echo $test->causes;
我还试图访问“buildsByBranchName”中的不同部分。我已经尝试了上述代码的许多不同变体,但我不断收到“Undefined property:stdClass”错误。您没有正确访问该值<代码>原因位于作为数组的
操作
下。您的代码也无法工作,因为原因是一个数组
// This is an array so you can't use echo here.
$causes = $test->actions[0]->causes;
// echo out the shortDescription
echo $causes[0]->shortDescription;
或
我刚刚看到您添加的内容,最后一行代码是有效的。另外,访问“buildsByBranchName”部分如何?我尝试了以下方法,但没有成功$version=“origin/release_5.6.0”;echo$test->actions[0]->causes[0]->buildsByBranchName[0]->$version->buildNumberbuildsByBranchName
不在下,导致仍然收到错误(未定义的属性:stdClass::$buildsByBranchName)。使用以下命令$version=“origin/release_5.6.0”;echo$test->actions[0]->buildsByBranchName->$version->buildNumber;我想我遇到了一个问题,因为buildsByBranchName上面的Json中有空括号。
echo $test->actions[0]->causes[0]->shortDescription;