dongtang7347 2018-05-12 03:10
浏览 172

将Json解析为php表导致未定义索引

I am accessing API for "ConnectWise". the data comes into JSON format. i was able to parse the data into table via PHP. however, empty fields in JSON results in Undefined index. This happens for some items with no website, or address for example. the rest shows up fine. Any help or input would be appreciated.

Here is my code to get the data from 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;
}

And to display the data:

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>";
            }
    }
   }
 }

Update - Json Sample

    [
    {
        "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",
        }
    }
]
  • 写回答

2条回答 默认 最新

  • duanju8308 2018-05-12 04:01
    关注

    json_decode() and https://jsonlint.com/ both complain about the comma after the value on lines 95 and 194. Removing them makes it valid json, after which your code works as soon as you remember to quote the key values for the associative array.

    I removed the two offending commas in the json and saved as a file, then added the quoting for your key values on the array, and finally removed the HTML table stuff and such (I'm just running it on command line to see output). Should be easy to put back in...

    <?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  "
    ".$account['id'] . "
                  " . $account['name'] . "
                  " . $account['addressLine1'] . "
                  " . $account['phoneNumber'] . "
                  " . $account['website'] . "
                  " . $account['name']."
    
    ";
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能