Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
json对象转换为字符串java_Java_Json_String - Fatal编程技术网

json对象转换为字符串java

json对象转换为字符串java,java,json,string,Java,Json,String,我有这样的json文件,我希望使用org.simple.json将json转换为字符串 { "header": { "IssuerID": "000141", "AuthenticationID": "e07020c0d040a050a0808099", "AuthenticationDateTime": "20151103093035", "AuthenticationDateTimeGMT": "20

我有这样的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);
 }
//json解析器数组untuk提取json ke字符串
JSONParser=新的JSONParser();
objectobj=parser.parse(输入);
JSONObject JSONObject=(JSONObject)对象;
JSONArray msg=(JSONArray)jsonObject.get(“header”);
//JSONArray数组=新的JSONArray();
//添加(“标题”);
迭代器迭代器=msg.Iterator();
系统输出打印项次(“标题”);
while(iterator.hasNext()){
JSONObject factObj=(JSONObject)迭代器.next();
IssuerID=(字符串)factObj.get(“IssuerID”);
System.out.println(“发卡机构ID:+IssuerID”);
AuthenticationID=(字符串)factObj.get(“AuthenticationID”);
System.out.println(“AuthenticationID:+AuthenticationID”);
AuthenticationDateTime=(字符串)factObj.get(“AuthenticationDateTime”);
System.out.println(“身份验证日期时间:+AuthenticationDateTime”);
AuthenticationDateTimeGMT=(字符串)factObj.get(“AuthenticationDateTimeGMT”);
System.out.println(“AuthenticationDateTimeGMT:+AuthenticationDateTimeGMT”);
签名=(字符串)factObj.get(“签名”);
System.out.println(“签名:”+签名);
}
//循环阵列
JSONArray msg1=(JSONArray)jsonObject.get(“body”);
系统输出打印项次(“正文”);
迭代器迭代器1=msg1.Iterator();
while(iterator1.hasNext()){
JSONObject factObj=(JSONObject)迭代器1.next();
TransactionDateTime=(字符串)factObj.get(“TransactionDateTime”);
System.out.println(“事务日期时间:+TransactionDateTime”);
TransactionID=(字符串)factObj.get(“TransactionID”);
System.out.println(“事务ID:+TransactionID”);
IAAuthenticationRequestReferenceId=(字符串)factObj.get(“IAAuthenticationRequestReferenceId”);
System.out.println(“IAAuthenticationRequestReferenceId:+IAAuthenticationRequestReferenceId”);
VerificationID=(字符串)factObj.get(“VerificationID”);
System.out.println(“VerificationID:+VerificationID”);
收单机构标识=(字符串)factObj.get(“收单机构标识”);
System.out.println(“收单机构id:+收单机构id”);
MerchantName=(字符串)factObj.get(“MerchantName”);
System.out.println(“商户名称:“+MerchantName”);
CurrencyCodeISO=(字符串)factObj.get(“CurrencyCodeISO”);
System.out.println(“CurrencyCodeISO:+CurrencyCodeISO”);
金额=(字符串)factObj.get(“金额”);
系统输出打印项次(“金额:+金额);
CardNumber=(字符串)factObj.get(“CardNumber”);
System.out.println(“卡号:+卡号”);
AuthenticationResponseCode=(字符串)factObj.get(“AuthenticationResponseCode”);
System.out.println(“身份验证响应代码:“+AuthenticationResponseCode”);
AuthenticationResponseDetails=(字符串)factObj.get(“AuthenticationResponseDetails”);
System.out.println(“身份验证响应详细信息:+AuthenticationResponseDetails”);
ValidityDateTimeExpireGMT=(字符串)factObj.get(“ValidityDateTimeExpireGMT”);
System.out.println(“有效日期时间到期GMT:+ValidityDateTimeExpireGMT”);
到期期限=(字符串)factObj.get(“到期期限”);
System.out.println(“到期日:“+到期日”);
}
我犯了这样的错误

java.lang.ClassCastException:org.json.simple.JSONObject不能为空 转换为org.json.simple.JSONArray Artajasa.Gateway.SubSystem.Process.RequestThread.run(RequestThread.java:636) 运行(Thread.java:745)

当我在页眉和正文前面添加[]时,我得到了正确的结果,
我的问题是它不是json数组而不是json对象。根据您的
json
结构和堆栈跟踪,有人能给我解决方案吗,
header
body
显然都是
JSONObject
s而不是
JSONArray
s这就是为什么当您尝试将它们转换为
JSONArray
时会出现此错误,请尝试将它们转换为
JSONObject
,而不是下一步:

JSONObject msg = (JSONObject) jsonObject.get("header");
...
JSONObject msg1 = (JSONObject) jsonObject.get("body");
要构建
JSONArray
我们使用方括号,请检查此项以了解更多详细信息。

JSONArray msg=(JSONArray)jsonObject.get(“header”)标头不是数组