gpt4 book ai didi

java - 如何在java中将响应解析为JSON

转载 作者:行者123 更新时间:2023-12-01 23:32:36 28 4
gpt4 key购买 nike

我使用以下代码来发布:

try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(VERIFY_PAYMENT_ACTIONURL);

// key is the parameter
// MERCHANT_KEY is the value
method.addParameter("form", "2");
method.addParameter("key", MERCHANT_KEY.trim());
method.addParameter("command", VERIFY_PAYMENT_COMMAND.trim());
method.addParameter("hash", hash);
method.addParameter("var1", transactionID.trim());
method.addParameter("salt", ALGORIHTM_SHA512_SALT_KEY.trim());

int statusCode = client.executeMethod(method);

if (statusCode != -1) {
in = method.getResponseBodyAsStream();
}


String text = method.getResponseBodyAsString();


System.out.println("text : "+text);

method.releaseConnection();

} catch (Exception e) {
e.printStackTrace();
}

我得到的响应文本如下:

a:3:{s:6:"status";i:1;s:3:"msg";s:44:"1 out of 1 Transactions Fetched Successfully";s:19:"transaction_details";a:1:{i:2298597;a:15:{s:8:"mihpayid";s:18:"403993715508098532";s:10:"request_id";N;s:12:"bank_ref_num";N;s:3:"amt";s:5:"53.77";s:4:"disc";s:4:"0.00";s:4:"mode";s:2:"CC";s:7:"PG_TYPE";s:4:"AXIS";s:7:"card_no";s:16:"512345XXXXXX2346";s:12:"name_on_card";s:3:"emu";s:4:"udf2";s:1:"0";s:7:"addedon";s:19:"2013-06-03 17:34:42";s:6:"status";s:7:"failure";s:14:"unmappedstatus";s:6:"failed";s:12:"Merchant_UTR";N;s:10:"Settled_At";N;}}}

现在我想提取上面的输出并将其放入 map 中,例如“transactionid”以及其他详细信息,例如以下格式

array('status' => '1',
'msg' => 'Transaction Fetched Successfully',
'transaction_details' =>
array(
'mihpayid' => Transaction ID,
'request_id' => Request ID,
'bank_ref_num' => Bank Reference Number,
'amt' => Amount
'disc' => Discount
'mode' => Transaction Mode (NB for Netbanking, CC for credit card, DC for Debit card, "-" for
'status' => Transaction Status

unknown)
)
);

我确实没有找到任何通用的方法来提取上述输出。有人会帮忙做这件事吗?

或者我可以将上面的内容转换为 JSON 并将它们放入上面这样的 map 中吗?

谢谢

最佳答案

我不认识它,但你确定它不是一种众所周知的格式吗?

要将其转换为 JSON,您需要1)删除后面跟着冒号(和冒号)的每个字符实例2)用逗号替换所有其他分号3)将所有其他分号替换为冒号您需要执行所有这些操作,同时考虑到字符串可能包含任何这些内容。

来自评论:该字符串采用 PHP 序列化形式。这个库解析它:https://code.google.com/p/serialized-php-parser/

关于java - 如何在java中将响应解析为JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19109701/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com