Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中从json字符串中获取值?_Php_Json - Fatal编程技术网

如何在php中从json字符串中获取值?

如何在php中从json字符串中获取值?,php,json,Php,Json,我试过这个代码:$record=$record['data']->Company但它总是以null返回。我还尝试了$record=$record[1]->data->Company这也会导致空值。我读了很多答案,尝试了很多解决方案,但都不适合我 一些比我更有经验的人请看一看,让我知道我哪里做错了 提前谢谢 { "1": { "index": 1, "data": { "CONTACTID": "3345923000000546002

我试过这个代码:$record=$record['data']->Company但它总是以null返回。我还尝试了$record=$record[1]->data->Company这也会导致空值。我读了很多答案,尝试了很多解决方案,但都不适合我

一些比我更有经验的人请看一看,让我知道我哪里做错了

提前谢谢

{
    "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"
        }
    }
}
我从下面的函数中获取json:

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();

        wp_mail( 'test@gmail.com', 'Post Data', $record );

        $payload = json_decode($record, true);

        $payload = $payload[1]['data']['Company'];

        file_put_contents(plugin_dir_path( __FILE__ ).'record.txt', $payload);

       return $payload;
    }
}

您可以尝试这样做:

<?php

$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"
        }
    }
}';
$payload = json_decode($json, true);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
    throw new RuntimeException("JSON decode error: $error");
}
var_dump($payload[1]['data']['Company']);
如果不需要关联数组而需要类,请尝试以下操作:

$payload = json_decode($json);
$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
    throw new RuntimeException("JSON decode error: $error");
}
var_dump($payload->{"1"}->data->Company);
结果是一样的

  • 你必须像这样解码记录

    $array=json_decode($record,true)

  • 然后您可以像这样访问数据

    echo$array['1']['data']['Company']

  • 在这里结账


    祝你有愉快的一天

    在data.json文件中包含json资源

    $json = file_get_contents('/tmp/data.json');
    
    或者一串

    $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"
            }
        }
    }';
    
    试试下面的方法

    $record = (array) json_decode($json);
    
    print_r($record[1]->data->Company);
    
    输出

    3ELLC
    
    这对我很有用:

    <?php
    $var = json_decode('{
        "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"
            }
        }
    }');
    
    foreach($var as $item) {
        echo $item->data->Company . '<br>';
    }
    ?>
    

    不幸的是,要获取属性名为数字的对象属性非常困难。最好将其解码为数组而不是对象<代码>$record=json_decode($str,true)
    @aynber当我使用json_decode时,我也会收到一个空值,所以我在postman中运行了你的答案,返回的值为空。我还应该提到,我正在函数中使用代码。请尝试一下,如果它不起作用,我会尽力帮助你。请按照SO标准格式化你的问题。你应该先打印$record,然后再尝试解码,并检查是否有任何数据。@urreeK在我的函数中,我返回$record,并在我的帖子中收到json,但是试图从json中获取值时,它总是空的。如果您使用var_dump($record),您会得到什么?工作得很好
    <?php
    $var = json_decode('{
        "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"
            }
        }
    }');
    
    foreach($var as $item) {
        echo $item->data->Company . '<br>';
    }
    ?>