1

我正在尝试修改来自 API(在本例中为 MYOB)的 json 响应,以便我可以更新 MYOB 中的记录。这需要我获取完整的 json 响应修改我需要的内容并将整个 json 响应放回 api。

如下操作 json 响应的最佳方法是什么。

{
"UID": "XX-XXXXX",
"Number": "00002024",
"Date": "2020-10-01T00:00:00",
"SupplierInvoiceNumber": "0001234",
"Supplier": {
    "UID": "XX-XXXXX",
    "Name": "Some Supplier",
    "DisplayID": "*None",
    "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
},
"ShipToAddress": "Some Supplier\r\n",
"Terms": {
    "PaymentIsDue": "InAGivenNumberOfDays",
    "DiscountDate": 0,
    "BalanceDueDate": 30,
    "DiscountForEarlyPayment": 0.0,
    "MonthlyChargeForLatePayment": 0.0,
    "DiscountExpiryDate": "2020-10-01T00:00:00",
    "Discount": 0.00,
    "DiscountForeign": null,
    "DueDate": "2020-10-31T00:00:00"
},
"IsTaxInclusive": true,
"IsReportable": false,
"Lines": [
    {
        "RowID": 215233,
        "Type": "Transaction",
        "Description": "DUMMY",
        "UnitOfMeasure": null,
        "UnitCount": null,
        "UnitPrice": null,
        "UnitPriceForeign": null,
        "DiscountPercent": null,
        "Total": 1.500000,
        "TotalForeign": null,
        "Account": {
            "UID": "xxx-xxx",
            "Name": "Office Expenses",
            "DisplayID": "1-2345",
            "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
        },
        "Job": null,
        "TaxCode": {
            "UID": "xxx-xxx",
            "Code": "GST",
            "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx"
        },
        "RowVersion": "6983429356563464192"
    }
],
"Subtotal": 1.000000,
"SubtotalForeign": null,
"Freight": 0.000000,
"FreightForeign": null,
"FreightTaxCode": {
    "UID": "xxx-xxx",
    "Code": "FRE",
    "URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx",
},
"TotalTax": 0.090000,
"TotalTaxForeign": null,
"TotalAmount": 1.500000,
"TotalAmountForeign": null,
"Category": null,
"Comment": "EZESCAN",
"ShippingMethod": null,
"PromisedDate": null,
"JournalMemo": "Purchase; Some Supplier",
"BillDeliveryStatus": "Nothing",
"AppliedToDate": 0.000000,
"AppliedToDateForeign": null,
"BalanceDueAmount": 1.000000,
"BalanceDueAmountForeign": null,
"Status": "Open",
"LastPaymentDate": null,
"Order": null,
"ForeignCurrency": null,
"CurrencyExchangeRate": null,
"URI": "https://ar2.api.myob.com/accountright/xxx-xxx/Contact/Supplier/xxx-xxx",
"RowVersion": "6262853416184184832"
}

我需要修改 Lines[0]->TotalForeign 以添加一个值 "$1.0000" 等。我真的不确定的部分是 "ForeignCurrency": null

我需要按以下方式修改它

"ForeignCurrency": {
    "UID": "xxx-xxx",
    "Code": "USD",
    "CurrencyName": "US Dollar",
    "CurrencyRate": 0.71428000,
    "URI": "https://ar2.api.myob.com/accountright/xxx-xx/GeneralLedger/Currency/xxx-xxx",
    "RowVersion": "-6496407278109851648"
},

我曾尝试使用字符串操作拆分 json,然后进行更改,然后重新编码 json,但这似乎不是正确的方法。

任何帮助将不胜感激。

谢谢

编辑

想我可能已经解决了。

<?php

$jsonobj = '{ As above reduce for easier reading}';

$arrayData = json_decode($jsonobj, true);

//One way
$replacementData = array('Lines' => array('0' => array('Total' =>'12345678')));
$newArrayData = array_replace_recursive($arrayData, $replacementData);

//Anotherway
$replacementData = [
  'UID'=> "xxx-xxx",
  'Code'=> "USD",
  'CurrencyName'=> "US Dollar",
  'CurrencyRate'=> 0.71428000,
  'URI'=> "https://ar2.api.myob.com/accountright/xxx-xxx/GeneralLedger/Currency/xxx-xxx",
  'RowVersion'=> "-6496407278109851648"
];

//Add the new content data.
$newArrayData['ForeignCurrency'] = $replacementData;

echo "<pre>"; 
echo json_encode($newArrayData, JSON_PRETTY_PRINT);
echo "</pre>";
4

0 回答 0