从数组中嵌套JSON数据

时间:2014-02-10 14:28:50

标签: json api format

我正在尝试从数组中嵌套JSON数据,因此它就像:

{
"subscriptionState" : "SUBSCRIBED",
"division" : {
    "value" : "Retail"
}
,
"attributes" : {
    "attributes" : [
        {
            "name" : "email",
            "value" : "john@doe.com"
        }
    ,
        {
            "name" : "firstName",
            "value" : "John"
        }
    ,
        {
            "name" : "lastName",
            "value" : "Doe"
        }
    ,
        {
            "name" : "postalCode",
            "value" : "97209"
        }
    ]
  }
}

目前我的数据如下:

{
"subscriptionState": "UNSUBSCRIBED",
"division": {
    "value": "UK"
},
"attributes": {
    "email": "karina@test.com",
    "firstName": "Karina",
    "lastName": "McG",
    "postCode": "BT93 EP3",
    "prefersMobile": "",
    "emailFormat": "HTML"
  }
}

代码:

$ym_api_url = 'https://connect.api.com/import';
$newsubscriber = array(
'subscriptionState' => 'UNSUBSCRIBED',
'division' => array(
'value' => 'UK'
),
'attributes' => array(
'firstName' => 'Karina',
'lastName' => 'McGourty',
'postCode' => 'BT93 EP3',
'prefersMobile' => '',
'emailFormat' => 'HTML'
)
);

这样做的正确方法是什么,到目前为止,我尝试了类似的方法:

'attributes' => array(
'attributes' => array(
(
'name' => 'email',
'value' => 'karina@test.com'
),
(
'name' => 'firstName',
'value' => 'Karina'
)

但没有快乐?

1 个答案:

答案 0 :(得分:0)

我最终想到了这一点,我知道这是一个简单的问题,但我想如果有人遇到这个问题,我会分享我的答案:

$newsubscriber = array(
'subscriptionState' => 'UNSUBSCRIBED',
'division' => array(
'value' => 'UK'
),
'attributes' => array(
'attributes' => array(
array(
'name' => 'email',
'value' => 'Karina@email.com'),
array(
'name' => 'firstName',
'value' => 'Karina'),
array(
'name' => 'lastName',
'value' => 'McG'),
array(
'name' => 'postalCode',
'value' => 'BT93 EP3'),
array(
'name' => 'prefersMobile',
'value' => '-1'),
array(
'name' => 'emailFormat',
'value' => 'HTML')
     )
   )
);