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

时间:2018-09-20 13:43:02

标签: php json

我尝试了以下代码: $ 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;
    }
}

4 个答案:

答案 0 :(得分:0)

您可以尝试执行以下操作:

<?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']);

结果,您将看到:

string(5) "3ELLC"

如果您不需要关联数组而是类,请尝试以下操作:

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

结果将相同。

答案 1 :(得分:0)

  1. 您必须像这样解码记录。

    $ array = json_decode($ record,true);

  2. 然后您可以像这样访问数据。

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

在此处https://www.tehplayground.com/UpHMPFxfSKdxXBmn

签出

祝你有美好的一天!

答案 2 :(得分:0)

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

答案 3 :(得分:0)

它对我有用:

<?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>';
}
?>

问题出在键“ 1”上,您不能像$var->1那样访问它,但是可以在数组中对其进行迭代。