dtpa98038 2017-06-05 03:25
浏览 192

如何创建双嵌套JSON对象?

my problem is how to add CUSTOM_FIELD or other double nested JSON like ADDRESS using PHP classes provided at Github/insightly:

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

I am trying to create the object from below 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",
      }
    ],
  }

However the data source is in Excel Format. So I use PHPExcel to convert it into JSON format and send it to another function that performs JSON ENCODE.

I am using Stdclass() like below:

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

but it doesn't work because they read it as a string not as an object, can anyone help me?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答