簡體   English   中英

json對象轉換為字符串java

[英]json object convert to string java

我有這樣的json文件,我想使用org.simple.json從json轉換為字符串

{
   "header": 
      {
         "IssuerID": "000141",
         "AuthenticationID": "e07020c0d040a050a0808099",
         "AuthenticationDateTime": "20151103093035",
         "AuthenticationDateTimeGMT": "20151103093035",
         "Signature": "MThEMTExQkMzQzM0OUIxQjM5MDc2MjFGMzMyQjhDNTk1OTI0NDNERTg5ODcwQjNFOTc0ODQwNThBNkQxNTgzNTk2N0YzN0I2OTkyMzI1QjY2OENDQjgxRUNERDlGNDFDNzVCMzQ5Njg5NTY4NzkwNUQ5MzBDN0ExOTVGOUY0OUY2QjlCQzlDQkREOEQ3NjEzRkQ2OEYyMDhEQTY2QTkzNUZDM0UzOTI3RDk2OTYwODg4NTkyNzYyQUJCQkJFREZGNzNCNEEyNUU5OTc5OTFFODk2MTQ0Q0Y4Q0RGNzg1M0JBQTM4QkZBQzRFRUY2MTkzM0E4REI3QkQ0MEJBRkU4OTlEOTVDNTkxOTQ0M0IwNjMyMzZDQ0U4MzdBQTQzODU3RkMyOEQ0Rjk2NUMyRkNERUM0NDREQkIzNUM0QUVERDUzRjFBOTk5RTQ4Mjk4MzNERjU3RTQ1QUE2Nzc4MDUyRTdERTdGRTVFRURGRkVGMTlFN0Y2QTAyQTVCNjk3NUU2OUNGRUU3MzRGNDZDOTE0Q0U3NTk5NzdGNDkyOTdFQkRGREIwNDBCNDhBQTkzMzE1QjU0MzI1Njk0MjgxMjVDMzM5OUM3MTExNTg4NENBOTU3QTM4OTU5MTY5OTQ1NkQ2NDNFOUVEMjU5MjRDNzhEMDQxMEJGM0ZEQUVFM0Y1QTU="
      },

   "body": 
      {
         "TransactionDateTime": "0151103093035",
         "TransactionID": " SP020110216",
         "IASauthenticationRequestReferenceID": "h0IrqhqBoUpUCiSv17NB0vHvABIUxHGCurJZFzUWdZJz9TxYio",
         "VerificationID": "h0IrqhqBoUpUCiSv17NB0vHvABIUxHGCurJZFzUWdZJz9TxYio ",
         "Acquirer_id": "000014",
         "MerchantName": " Twin Hotel ",
         "CurrencyCodeISO": "360",
         "Amount": "970000.00",
         "CardNumber": " BmaHLuFRg4SaPnaGB07t5fNrHXr/lyOa ",
         "AuthenticationResponseCode": "00",
         "AuthenticationResponseDetails": "Success",
         "ValidityDateTimeExpireGMT": "20151103094035",
         "Expiry_Period": "10"
      }
}

當我使用此腳本進行轉換時

    //  json parser array untuk extract json ke string 
JSONParser parser = new JSONParser();
Object obj = parser.parse(input);
JSONObject jsonObject = (JSONObject) obj;

 JSONArray msg = (JSONArray) jsonObject.get("header");
//JSONArray array = new JSONArray();
//array.add("header");
 Iterator<JSONObject> iterator = msg.iterator();
                System.out.println("Header ");       
 while (iterator.hasNext()) {
 JSONObject factObj = (JSONObject) iterator.next();
  IssuerID = (String) factObj.get("IssuerID");
 System.out.println("Issuer ID : " + IssuerID);

  AuthenticationID = (String) factObj.get("AuthenticationID");
 System.out.println("AuthenticationID : " +AuthenticationID);

  AuthenticationDateTime = (String) factObj.get("AuthenticationDateTime");
 System.out.println("Authentication Date Time : " + AuthenticationDateTime);

  AuthenticationDateTimeGMT = (String) factObj.get("AuthenticationDateTimeGMT");
 System.out.println("AuthenticationDateTimeGMT : " + AuthenticationDateTimeGMT);

 Signature = (String) factObj.get("Signature");
 System.out.println("Signature : " + Signature);
 }
  //loop array
 JSONArray msg1 = (JSONArray) jsonObject.get("body");

                System.out.println("Body");
 Iterator<JSONObject> iterator1 = msg1.iterator();

 while (iterator1.hasNext()) {
 JSONObject factObj = (JSONObject) iterator1.next();

 TransactionDateTime = (String) factObj.get("TransactionDateTime");
 System.out.println("Transaction DateTime: " + TransactionDateTime );

 TransactionID = (String) factObj.get("TransactionID");
 System.out.println("Transaction ID : " + TransactionID);

   IASauthenticationRequestReferenceID = (String) factObj.get("IASauthenticationRequestReferenceID");
 System.out.println("IASauthenticationRequestReferenceID : " + IASauthenticationRequestReferenceID);

   VerificationID  = (String) factObj.get("VerificationID");
 System.out.println("VerificationID : " + VerificationID);


 Acquirer_id= (String) factObj.get("Acquirer_id");
 System.out.println("Acquirer id : " + Acquirer_id);

  MerchantName = (String) factObj.get("MerchantName");
 System.out.println("Merchant Name: " + MerchantName);

CurrencyCodeISO = (String) factObj.get("CurrencyCodeISO");
 System.out.println("CurrencyCodeISO : " + CurrencyCodeISO);

 Amount = (String) factObj.get("Amount");
 System.out.println("Amount : " + Amount);

 CardNumber= (String) factObj.get("CardNumber");
 System.out.println("Card Number : " + CardNumber);

  AuthenticationResponseCode = (String) factObj.get("AuthenticationResponseCode");
 System.out.println("Authentication Response Code: " + AuthenticationResponseCode);

   AuthenticationResponseDetails = (String) factObj.get("AuthenticationResponseDetails");
 System.out.println("Authentication Response Detail : " + AuthenticationResponseDetails);

   ValidityDateTimeExpireGMT    = (String) factObj.get("ValidityDateTimeExpireGMT");
System.out.println("Validity DateTime Expire GMT : " + ValidityDateTimeExpireGMT);

  Expiry_Period = (String) factObj.get("Expiry_Period");
 System.out.println("Expiry Period : " + Expiry_Period);
 }

我有這樣的錯誤

java.lang.ClassCastException:無法將java.lang.Thread上的org.json.simple.JSONObject強制轉換為Artajasa.Gateway.SubSystem.Process.RequestThread.run(RequestThread.java:636)上的org.json.simple.JSONArray。運行(Thread.java:745)

當我在標題和正文前面添加[]時,我得到了正確的結果,我的問題是不是json數組插入了json對象,任何人都可以給我解決方案

根據您的JSON結構和堆棧跟蹤, headerbody顯然都是JSONObject而不是JSONArray ,這就是為什么當您嘗試將它們JSONArrayJSONArray ,嘗試將其JSONObjectJSONObject而不是下一個時出現此錯誤的原因:

JSONObject msg = (JSONObject) jsonObject.get("header");
...
JSONObject msg1 = (JSONObject) jsonObject.get("body");

要構建JSONArray ,請使用方括號 ,有關更多詳細信息, JSONArray 簡單的JSON簡介

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM