将JSON层次结构读取到目标字段并循环遍历每个结果

时间:2016-05-16 19:00:43

标签: php json loops hierarchy

我正在使用Zoho CRM API查看联系人。我以JSON格式返回响应。这是我的代码:

$token="03430403423mysecrettokenhere03403";
$url = "https://crm.zoho.com/crm/private/json/Leads/getRecords?";
$url .= "authtoken=".$token."&scope=crmapi";
$json = file_get_contents($url);
$obj = json_decode($json);
echo "<pre>";
print_r($obj);
echo "</pre>";

foreach ($obj as $testme) {
    $test = $obj->results[0]->Company;
    echo '<br><br>';
    echo "test: " . $test;
}

原始回复:

stdClass Object
(
[response] => stdClass Object
    (
        [result] => stdClass Object
            (
                [Leads] => stdClass Object
                    (
                        [row] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [no] => 1
                                        [FL] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [content] => 1900238000000103005
                                                        [val] => LEADID
                                                    )

                                                [1] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => SMOWNERID
                                                    )

                                                [2] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Lead Owner
                                                    )

                                                [3] => stdClass Object
                                                    (
                                                        [content] => Testttt
                                                        [val] => Company
                                                    )

                                                [4] => stdClass Object
                                                    (
                                                        [content] => Test
                                                        [val] => First Name
                                                    )

                                                [5] => stdClass Object
                                                    (
                                                        [content] => Lead
                                                        [val] => Last Name
                                                    )

                                                [6] => stdClass Object
                                                    (
                                                        [content] => testlead@test.com
                                                        [val] => Email
                                                    )

                                                [7] => stdClass Object
                                                    (
                                                        [content] => 0
                                                        [val] => No of Employees
                                                    )

                                                [8] => stdClass Object
                                                    (
                                                        [content] => 0
                                                        [val] => Annual Revenue
                                                    )

                                                [9] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => SMCREATORID
                                                    )

                                                [10] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Created By
                                                    )

                                                [11] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => MODIFIEDBY
                                                    )

                                                [12] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Modified By
                                                    )

                                                [13] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 16:04:37
                                                        [val] => Created Time
                                                    )

                                                [14] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 16:04:37
                                                        [val] => Modified Time
                                                    )

                                                [15] => stdClass Object
                                                    (
                                                        [content] => false
                                                        [val] => Email Opt Out
                                                    )

                                                [16] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 16:04:37
                                                        [val] => Last Activity Time
                                                    )

                                            )

                                    )

                                [1] => stdClass Object
                                    (
                                        [no] => 2
                                        [FL] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [content] => 1900238000000100023
                                                        [val] => LEADID
                                                    )

                                                [1] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => SMOWNERID
                                                    )

                                                [2] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Lead Owner
                                                    )

                                                [3] => stdClass Object
                                                    (
                                                        [content] => Test
                                                        [val] => First Name
                                                    )

                                                [4] => stdClass Object
                                                    (
                                                        [content] => Lead
                                                        [val] => Last Name
                                                    )

                                                [5] => stdClass Object
                                                    (
                                                        [content] => testlead@test.com
                                                        [val] => Email
                                                    )

                                                [6] => stdClass Object
                                                    (
                                                        [content] => 0
                                                        [val] => No of Employees
                                                    )

                                                [7] => stdClass Object
                                                    (
                                                        [content] => 0
                                                        [val] => Annual Revenue
                                                    )

                                                [8] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => SMCREATORID
                                                    )

                                                [9] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Created By
                                                    )

                                                [10] => stdClass Object
                                                    (
                                                        [content] => 1900238000000097003
                                                        [val] => MODIFIEDBY
                                                    )

                                                [11] => stdClass Object
                                                    (
                                                        [content] => chase
                                                        [val] => Modified By
                                                    )

                                                [12] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 13:15:29
                                                        [val] => Created Time
                                                    )

                                                [13] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 13:15:29
                                                        [val] => Modified Time
                                                    )

                                                [14] => stdClass Object
                                                    (
                                                        [content] => false
                                                        [val] => Email Opt Out
                                                    )

                                                [15] => stdClass Object
                                                    (
                                                        [content] => 2016-05-16 13:15:29
                                                        [val] => Last Activity Time
                                                    )

                                            )

                                    )

                            )

                    )

            )

        [uri] => /crm/private/json/Leads/getRecords
    )

)

test:

问题1:

如何阅读层次结构并定位“公司”和“名字”“姓氏”等字段。我希望能够为每个联系人输出这些字段。

$test = $obj->response->results->Accounts->row;

这是我能得到的。

问题2:

我目前有两个联系人在那里进行测试。我怎样才能遍历每一个?

foreach ($obj as $testme) {
    $test = $obj->results[0]->Company;
    echo '<br><br>';
    echo "test: " . $test;
}

上面的当前循环只返回一个对象。

1 个答案:

答案 0 :(得分:1)

好的,假设这些JSON返回数组总是以相同的方式返回,你可以做这样的事情。首先,通过剥离所有包装垃圾来清理返回对象:

$cleanObj = $return->response->results->Leads->row;

然后使用一个简单的函数遍历每个联系人的数组:

function getCompany($obj, $part) {
    $innerObj = $obj->FL;
    foreach ($innerObj as $c)  {
        if ($c['val'] == $part) {
            return $c['content'];
        }
    }
}

现在您可以传入其中一个联系人和所需的部分。

$company = getCompany($cleanObj[0], "Company");

未经测试(主要是因为我不想复制数据转储),但这应该抓住&#34; Testttt&#34;从你的第一次接触。

关于第二个问题,您只需循环访问$cleanObj数组即可访问所有联系人。不是$cleanObj[0],而是$cleanObj[1]等等。