使用php

时间:2017-02-13 13:12:21

标签: php json

我正在获取下面的JSON输出:

{
"clientCorrelator": "58a1acaf3ebf0",
"referenceCode": "REF-12345",
"endUserId": "263774705932",
"transactionOperationStatus": "Charged",
"paymentAmount": {
    "0": {
        "amount": 34,
        "currency": "USD",
        "description": "Ecofarmer Bulk Sms Online payment"
    }
},
"chargeMetaData": {
    "channel": "WEB",
    "purchaseCategoryCode": "Online Payment",
    "onBeHalfOf": "Paynow Topup"
},
"merchantCode": "42467",
"merchantPin": "1357",
"merchantNumber": "771999313"

}

我想得到下面的输出但不知何故我的php到JSON对象转换正在转变"充电信息"关键是" 0"。

$payment_amount =  array(
$charginginformation = array(
  'amount' => 34,
  'currency' => 'USD',
  'description' => 'Ecofarmer Bulk Sms Online payment'
  )

  );

$charge_data = array(
  'channel' => 'WEB',
  'purchaseCategoryCode' => 'Online Payment',
  'onBeHalfOf' => 'Paynow Topup'
);


//API Url
$url = '';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
  'clientCorrelator' => $u_id,
  'referenceCode' => 'REF-12345',
  'endUserId' => '263774705932',
  'transactionOperationStatus' => 'Charged',
  'paymentAmount' => $payment_amount,
  'chargeMetaData' => $charge_data,
  'merchantCode' => '42467',
  'merchantPin' => '1357',
  'merchantNumber' => '771999313'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData, JSON_FORCE_OBJECT);

如何阻止json_encode更改密钥?

1 个答案:

答案 0 :(得分:1)

您的问题在这里:

$payment_amount =  array(
    //this is essentially array("cat", "dog", "etc");
    $charginginformation = array(
        'amount' => 34,
        'currency' => 'USD',
        'description' => 'Ecofarmer Bulk Sms Online payment'
    )
);

您正在向具有数字索引

的数组添加元素

要使其发挥作用

$payment_amount =  array(
    "charginginformation" => array(
        //array data
    )
);