如何创建双嵌套JSON对象?

时间:2017-06-05 03:25:17

标签: php json object nested phpexcel

我的问题是如何使用Github / insightly提供的PHP类添加CUSTOM_FIELD或其他双嵌套JSON,如ADDRESS

$contact = (object)array('ADDRESSES' =>(object) array('ADDRESS_TYPE' => 'POSTAL','COUNTRY' => $data[4])); 
$contact = $this->addContact($contact); 
$data = json_encode($contact); 
print_r($data); 

我正在尝试从JSON下面创建对象:

{   
    "FIRST_NAME": "Andy",
    "LAST_NAME": "Nguyen",
    "BACKGROUND": "McD is a fast food company with lot of franchies around the world",
    "VISIBLE_TO": "EVERYONE",
    "CUSTOMFIELDS": [
      {
        "CUSTOM_FIELD_ID": "CONTACT_FIELD_1",
        "FIELD_VALUE": "Industry"
      },
      {
      "CUSTOM_FIELD_ID": "CONTACT_FIELD_2",
        "FIELD_VALUE": "Range of Investment"
      },
      {
        "CUSTOM_FIELD_ID": "CONTACT_FIELD_3",
        "FIELD_VALUE": "https://www.McD.com"
      },
      {
        "CUSTOM_FIELD_ID": "CONTACT_FIELD_4",
        "FIELD_VALUE": "https://www.dropbox.com/s/u5vykgsxhfi7x6c/McD%202017%20ASEAN%20Food%20In%20Asia.pdf?dl=3"
      }
    ],
    "ADDRESSES": [
      {
        "ADDRESS_TYPE": "POSTAL", 
        "COUNTRY": "Malaysia"
      }
    ],
    "CONTACTINFOS": [
      {
        "TYPE": "EMAIL",
        "LABEL": "WORK",
        "DETAIL": "Andy@Mcd.com"
      }
    ],
    "LINKS": [
      {
        "OPPORTUNITY_ID": null,
        "ORGANISATION_ID": 106787363,
        "ROLE": "CEO",
      }
    ],
  }

但是数据源是Excel格式。所以我使用PHPExcel将其转换为JSON格式并将其发送到另一个执行JSON ENCODE的函数。

我正在使用Stdclass(),如下所示:

$contact = new stdClass();
$contact->CUSTOMFIELDS -> CUSTOM_FIELD_ID = "CONTACT_FIELD_1";
$contact->CUSTOMFIELDS -> CUSTOM_FIELD_ID = $data;

但它不起作用,因为他们把它作为一个字符串而不是一个对象来读,有人可以帮助我吗?

0 个答案:

没有答案