json object convert to string java -
i have json file , want convert json string using org.simple.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" } }
when convert using script
// 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); }
i got error
java.lang.classcastexception: org.json.simple.jsonobject cannot cast org.json.simple.jsonarray @ artajasa.gateway.subsystem.process.requestthread.run(requestthread.java:636) @ java.lang.thread.run(thread.java:745)
when add [ ] in front of header , body got correct result, problem it's not json array insted json object can give me solution
according json
structure , stack trace, header
, body
both jsonobject
s not jsonarray
s why error when try cast them jsonarray
, try cast them jsonobject
instead next:
jsonobject msg = (jsonobject) jsonobject.get("header"); ... jsonobject msg1 = (jsonobject) jsonobject.get("body");
to build jsonarray
use square brackets, check simple json
introduction more details.
Comments
Post a Comment