简体   繁体   中英

how to parse the Response as JSON in java

I use the following code to post :

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();
        } 

and I get the response text as follows :

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;}}}

now I want to extract the above output and put it into map like "transactionid" and other details like the following format

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)
)
);

I really do not find any common way to extract the above output. will some one help to do this?

or is it possible for me to convert the above to JSON and put them in to a map like the above?

thanks

I don't recognise it but are you sure it's not a well know format?

To turn it into JSON you'd need to 1)remove every instance of a character that is followed by a colon (and the colon) 2)replace every other semi-colons with a comma 3)replace all the other semi-colons with colons And you'd need to do all that while taking into account that strings could contain any of those things.

From comments: The string is in PHP serialised form. This library parses it: https://code.google.com/p/serialized-php-parser/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM