2013-10-01 54 views
0

我使用下面的代碼發佈:如何解析Java中的響應作爲JSON

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

現在我想提取上述輸出並將其放入地圖,如「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並將它們放入上面的地圖中?

謝謝

回答

3

我不承認它,但你確定它不是一個知名的格式?

爲了把它變成JSON你需要 1)除去後跟一個冒號的字符的每個實例(和結腸) 2)用逗號 3取代每隔分號)替換所有其他含冒號的分號 而且您需要做所有這些,同時考慮到字符串可能包含任何這些內容。

來自評論: 該字符串是PHP序列化的形式。這個庫解析它:https://code.google.com/p/serialized-php-parser/

+0

它是'PHP serialized string'。與供應商+1討論您的努力 –

+1

嗯,這個庫會解析PHP序列化表單:https://code.google.com/p/serialized-php-parser/ – tom

+0

完全一樣幫助我完成任務。謝謝 –