繁体   English   中英

为什么json_decode输出始终为null?

[英]Why is json_decode output always null?

我有一个问题,当我在下面的函数中执行返回$ record时 ,在邮递员漂亮的视图中获得有效的json,但是如果我尝试print_r($ record),则json末尾为null。

然后,当我尝试json_decode时 ,出于某种原因输出总是为null

如果有人对为什么会这样有任何想法,我非常感谢您的投入。

谢谢

function webhook_listener($request_data){
        $client = new ZohoCRMClient('Contacts', 'API key Here');

        $parameters = $request_data->get_params();

        if( !isset( $parameters['contactId'] ) || empty($parameters['contactId']) ){
            file_put_contents(plugin_dir_path( __FILE__ ).'invalid.txt', 'No parameter found');
            return array( 'error' => 'no_parameter_given' );
        }else{
            $companyid = $parameters['contactId'];

            //file_put_contents(plugin_dir_path( __FILE__ ).'crm.txt', $parameters);

            $record = $client->getRecordById()->id($companyid)->request();

            $record = json_decode($record);

            $error = json_last_error();
            if ($error !== JSON_ERROR_NONE) {
                throw new RuntimeException("JSON decode error: $error");
            }

            echo $company = $record[1]['data']['Company'];

        }

    }

JSON:

{"1":{"index":1,"data":{"CONTACTID":"3345923000000546002","SMOWNERID":"3345923000000158021","Contact Owner":"Frank Rosa","First Name":"Administrator","Last Name":"Ian","Email":"poojarajan3ellc@gmail.com","Created Time":"2018-09-19 14:32:35","Modified Time":"2018-09-20 02:48:51","Full Name":"Administrator Ian","Description":"Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard.","Last Activity Time":"2018-09-20 02:48:51","Instagram Url":"http:\/\/www.instagram.com\/3e_llc","Company":"3ELLC","Website":"https:\/\/www.3ellc.org","Phone_1":"(727) 420-1050","Full Address":"2152 Arcadia Rd, Holiday, FL 34690, USA","Facebook Url":"http:\/\/www.facebook.com\/3eLLC\/","Logo Url":"https:\/\/dev.energypages.com\/wp-content\/uploads\/2018\/05\/header-logo-57.png","Twitter Url":"http:\/\/www.twitter.com\/3e_llc","Membership Level":"Basic","Select Service":"Technology","User ID":"347"}}}

var_dump输出:

array(1) {
  [1]=>
  object(CristianPontes\ZohoCRMClient\Response\Record)#2068 (2) {
    ["index"]=>
    int(1)
    ["data"]=>
    array(22) {
      ["CONTACTID"]=>
      string(19) "3345923000000546002"
      ["SMOWNERID"]=>
      string(19) "3345923000000158021"
      ["Contact Owner"]=>
      string(10) "Frank Rosa"
      ["First Name"]=>
      string(13) "Administrator"
      ["Last Name"]=>
      string(3) "Ian"
      ["Email"]=>
      string(25) "poojarajan3ellc@gmail.com"
      ["Created Time"]=>
      string(19) "2018-09-19 14:32:35"
      ["Modified Time"]=>
      string(19) "2018-09-20 02:48:51"
      ["Full Name"]=>
      string(17) "Administrator Ian"
      ["Description"]=>
      string(407) "Equity and Empowerment through Education. It is the Mission of 3e LLC to promote equity and empowerment for all students through engaging professional development for educators and parents, one-on-one coaching for teacher efficacy, and mentoring services for youth to promote enrichment and success. For the empowered, we offer editing, transcribing, and ghostwriting services to ensure your voice is heard."
      ["Last Activity Time"]=>
      string(19) "2018-09-20 02:48:51"
      ["Instagram Url"]=>
      string(31) "http://www.instagram.com/3e_llc"
      ["Company"]=>
      string(5) "3ELLC"
      ["Website"]=>
      string(21) "https://www.3ellc.org"
      ["Phone_1"]=>
      string(14) "(727) 420-1050"
      ["Full Address"]=>
      string(39) "2152 Arcadia Rd, Holiday, FL 34690, USA"
      ["Facebook Url"]=>
      string(30) "http://www.facebook.com/3eLLC/"
      ["Logo Url"]=>
      string(73) "https://dev.energypages.com/wp-content/uploads/2018/05/header-logo-57.png"
      ["Twitter Url"]=>
      string(29) "http://www.twitter.com/3e_llc"
      ["Membership Level"]=>
      string(5) "Basic"
      ["Select Service"]=>
      string(10) "Technology"
      ["User ID"]=>
      string(3) "347"
    }
  }
}
null

上面的输出是邮递员的原始视图。 在漂亮的视图中,我收到此错误(意外的“ a”)

此处有关如何解析jSON的所有答案都是正确的。 但是我在使用php库链接到zoho时,该链接以不同的方式处理数据,因此我必须对来自API的响应进行foreach,然后使用json_encode()数据和json_decode()来获取值。

我正在使用的图书馆: https : //github.com/cristianpontes/zoho-crm-client-php

foreach($record as $records){
            $payload = $records->getData();
            $payload = json_encode($payload);
            $payload = json_decode($payload, true);

            $error = json_last_error();
            if ($error !== JSON_ERROR_NONE) {
                throw new RuntimeException("JSON decode error: $error");
            }

            $company = $payload['Company'];
}

感谢所有人,尤其是@ miken32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM