Json API响应操作PHP

问题描述

我正在尝试从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","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,"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>";

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...