我只想知道如何将这个API响应转换为json数据。我没有API的访问权限。我尝试使用explode PHP,但是响应似乎分成了数组。
StatCode: 11
StatName: cancelled
OrderID: OP_AFIFAH_6889
Amount: 1.10
TranID: 143519171
Domain: test
BillingDate: 2019-11-26 16:58:49
BillingName: test
VrfKey: test
Channel: credit
Currency: MYR
ErrorCode: CC_000
ErrorDesc: Successful Payment :
我想实现这样的目标
{
"StatCode": 11,
"StatName": "cancelled",
"OrderID": "OP_AFIFAH_6889",
"Amount": 1.10,
"TranID": 143519171
etc..
}
谢谢
答案 0 :(得分:0)
一些简单的RegExp怎么样?
var src = 'StatCode: 11\n' +
'StatName: cancelled\n' +
'OrderID: OP_AFIFAH_6889\n' +
'Amount: 1.10\n' +
'TranID: 143519171\n' +
'Domain: test\n' +
'BillingDate: 2019-11-26 16:58:49\n' +
'BillingName: test\n' +
'VrfKey: test\n' +
'Channel: credit\n' +
'Currency: MYR\n' +
'ErrorCode: CC_000\n' +
'ErrorDesc: Successful Payment :\n';
if (src[src.length-1] == '\n') src = src.substr(0, src.length-1);
var dest = src.replace(/([^:]+:\s*)/gm, '$1 "').replace(/\s*([^\n]+)\n/gm, '$1",\n')
console.log('{' + dest + '}')
{
"StatCode": 11,
"StatName": "cancelled",
"OrderID": "OP_AFIFAH_6889",
"Amount": 1.10,
"TranID": 143519171
etc..
}