簡體   English   中英

如何從php中的json字符串獲取值?

[英]How to get a value from a json string in php?

我已經試過了這段代碼: $ 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
$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那樣訪問它,但是可以在數組中對其進行迭代。

您可以嘗試執行以下操作:

<?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. 您必須像這樣解碼記錄。

    $ array = json_decode($ record,true);

  2. 然后,您可以像這樣訪問數據。

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

在這里簽出, https://www.tehplayground.com/UpHMPFxfSKdxXBmn

祝你有美好的一天!

在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM