1

我正在访问“ConnectWise”的 API。数据为 JSON 格式。我能够通过 PHP 将数据解析成表格。但是,JSON 中的空字段会导致未定义索引。例如,某些没有网站或地址的项目会发生这种情况。其余的都很好。任何帮助或输入将不胜感激。

这是我从 Connectwise 获取数据的代码:

function get_companies(){

   $curl = curl_init();
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
   curl_setopt($curl, CURLOPT_URL, "https://api- 
   na.myconnectwise.net/v4_6_release/apis/3.0/company/companies");
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(

  "Authorization: Basic (OUR KEY)",

   'Content-type: application/json'

       ));

$result = curl_exec($curl);
curl_close($curl);
$decoded =  json_decode($result,true);
return $decoded;
}

并显示数据:

function list_all_accounts(){

    $accounts = get_companies();

    if ( !empty ($accounts)){
            foreach ($accounts as $account) {

                {
        echo "
                    </td>
                    <td>
                        $account[id]
                    </td>
                    <td>
                        $account[name]
                    </td>
                    <td>
                        $account[addressLine1]
                    </td>
                    <td>
                         $account[phoneNumber]
                    </td>
                    <td>
                         $account[website]
                    </td>   
                    <td>
                        $account[name]
                    </td>                       
                    </tr>";
            }
    }
   }
 }

更新 - Json 示例

    [
    {
        "id": 250,
        "identifier": "company name ",
        "name": "company name",
        "status": {
            "id": 1,
            "name": "Active",
            "_info": {
                "status_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/statuses/1"
            }
        },
        "type": {
            "id": 1,
            "name": "Client",
            "_info": {
                "type_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/types/1"
            }
        },
        "addressLine1": "address line 1",
        "city": "New York",
        "state": "NY",
        "zip": "11111",
        "country": {
            "id": 1,
            "name": "United States",
            "_info": {
                "country_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/countries/1"
            }
        },
        "phoneNumber": "123456789",
        "faxNumber": "",
        "website": "www.site.com",
        "territoryId": 2,
        "accountNumber": "",
        "dateAcquired": "2006-06-21T04:00:00Z",
        "sicCode": {
            "id": 1209,
            "name": "consulting"
        },
        "annualRevenue": 0,
        "timeZone": {
            "id": 1,
            "name": "GMT-5/Eastern Time: US & Canada",
            "_info": {
                "timeZoneSetup_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/system/timeZoneSetups/1"
            }
        },
        "leadFlag": false,
        "unsubscribeFlag": false,
        "userDefinedField5": "1",
        "taxCode": {
            "id": 8,
            "name": "Tax-State",
            "_info": {
                "taxCode_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/finance/taxCodes/8"
            }
        },
        "billingTerms": {
            "id": 1,
            "name": "Net 30 days"
        },
        "billToCompany": {
            "id": 250,
            "identifier": "comp1 ",
            "name": "company1.",
            "_info": {
                "company_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/250"
            }
        },
        "billingSite": {
            "id": 1291,
            "name": "company1",
            "_info": {
                "site_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies"
            }
        },
        "invoiceDeliveryMethod": {
            "id": 1,
            "name": "Mail"
        },
        "deletedFlag": false,
        "mobileGuid": "1df91371-6d7a-4778-ab81-f3e7761f5211",
        "currency": {
            "id": 7,
            "symbol": "$",
            "isoCode": "USD",
            "name": "US Dollars",
            "_info": {
                "currency_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/finance/currencies/7"
            }
        },
        "_info": {
            "lastUpdated": "2018-04-02T16:36:05Z",
            "updatedBy": "user1",
            "dateEntered": "2006-06-21T16:04:59Z",
        }
    },
     {
        "id": 250,
        "identifier": "company name ",
        "name": "company name",
        "status": {
            "id": 1,
            "name": "Active",
            "_info": {
                "status_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/statuses/1"
            }
        },
        "type": {
            "id": 1,
            "name": "Client",
            "_info": {
                "type_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/types/1"
            }
        },
        "addressLine1": "address line 1",
        "city": "New York",
        "state": "NY",
        "zip": "11111",
        "country": {
            "id": 1,
            "name": "United States",
            "_info": {
                "country_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/countries/1"
            }
        },
        "phoneNumber": "123456789",
        "faxNumber": "",
        "website": "www.site.com",
        "territoryId": 2,
        "accountNumber": "",
        "dateAcquired": "2006-06-21T04:00:00Z",
        "sicCode": {
            "id": 1209,
            "name": "consulting"
        },
        "annualRevenue": 0,
        "timeZone": {
            "id": 1,
            "name": "GMT-5/Eastern Time: US & Canada",
            "_info": {
                "timeZoneSetup_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/system/timeZoneSetups/1"
            }
        },
        "leadFlag": false,
        "unsubscribeFlag": false,
        "userDefinedField5": "1",
        "taxCode": {
            "id": 8,
            "name": "Tax-State",
            "_info": {
                "taxCode_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/finance/taxCodes/8"
            }
        },
        "billingTerms": {
            "id": 1,
            "name": "Net 30 days"
        },
        "billToCompany": {
            "id": 250,
            "identifier": "comp1 ",
            "name": "company1.",
            "_info": {
                "company_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies/250"
            }
        },
        "billingSite": {
            "id": 1291,
            "name": "company1",
            "_info": {
                "site_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/company/companies"
            }
        },
        "invoiceDeliveryMethod": {
            "id": 1,
            "name": "Mail"
        },
        "deletedFlag": false,
        "mobileGuid": "1df91dd371-6d7addd-4778s-ab81-f3e7761f5211",
        "currency": {
            "id": 7,
            "symbol": "$",
            "isoCode": "USD",
            "name": "US Dollars",
            "_info": {
                "currency_href": "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/finance/currencies/7"
            }
        },
        "_info": {
            "lastUpdated": "2018-04-02T16:36:05Z",
            "updatedBy": "user1",
            "dateEntered": "2006-06-21T16:04:59Z",
            "enteredBy": "CONVERSION",
        }
    }
]
4

2 回答 2

0

根据您的评论,您在 HTML 中使用的某些属性可能不是您收到的 JSON 的一部分。这意味着您正在访问未设置的数组索引,例如使用$account[website].

我看到了两种解决方案:

  1. 仅在数组中设置数据时才输出数据:

    echo '<td>'.(isset($account['website']) ? $account['website'] : '').'</td>';
    
  2. 使用具有所有值的默认值的数组作为$account数组的基础并合并它们:

    $base = [
        'website' => 'no website',
        'phoneNumber' => '',
    ];
    
    foreach ($accounts as $account) {
        // this will override all elements of $base with them of $account,
        // but only if they are present in $account
        $account = array_merge($base, $account);
    
        // ... your output here ...
    }
    
于 2018-05-12T09:10:33.013 回答
0

json_decode()https://jsonlint.com/都抱怨第 95 行和第 194 行的值后面的逗号。删除它们使其成为有效的 json,之后只要您记得引用关联数组的键值,您的代码就会工作。

我删除了 json 中的两个有问题的逗号并保存为一个文件,然后在数组中添加了对键值的引用,最后删除了 HTML 表等内容(我只是在命令行上运行它以查看输出) . 应该很容易装回去...

<?php

function get_companies() {
    $j = file_get_contents("json.json");
    $jd = json_decode($j, true);
    return $jd;
}

$accounts = get_companies();
if (!empty($accounts)) {
    foreach ($accounts as $account) {
        echo  "\n".$account['id'] . "
              " . $account['name'] . "
              " . $account['addressLine1'] . "
              " . $account['phoneNumber'] . "
              " . $account['website'] . "
              " . $account['name']."\n\n";
    }
}
?>
于 2018-05-12T04:01:09.313 回答