Home > OS >  Android: Unable to convert string to JsonArray
Android: Unable to convert string to JsonArray

Time:07-27

I have a json which is getting returned as array but I'm unable to convert it into a list. The string looks like:

"[{\"@context\":[\"v1\"],\"id\":\"did\",\"type\":[\"VerifiableCredential\"],\"issuer\":\"did\",\"issuanceDate\":\"2022-07-27T10:24:48Z\",\"expirationDate\":\"2023-07-27T10:24:48Z\",\"credentialSchema\":{\"id\":\"didversion=0.2\",\"type\":\"JsonSchemaValidator2018\"},\"credentialSubject\":{\"contact\":\"[email protected]\",\"name\":\"Fedex\",\"orgId\":\"fedex\",\"privacyPolicy\":\"privacy-policy\",\"services\":[{\"purposes\":[{\"purpose\":\"To get my Health Insights data into my wallet.\"}],\"service\":\"download\"}],\"technical\":{\"download\":{\"id\":\"DOQQ163002\",\"linkId\":\"c97b8411-a6ba-4955-88e7-071353fbc2b8\",\"passcode\":\"2a57c0cd-b036-4c6f-9a41-adde8be8cc3e\",\"url\":\"attachments\"},\"symmetricKey\":{\"algorithm\":\"aes-256-cbc\",\"iv\":\"8qbl7cgPpuVzGrUSmt/Vsw==\",\"value\":\"3ROXMpHAEO8FNSVMzkTZIjnbFE7lD4T1FgwWs3B83JA=\"}},\"type\":\"profile\",\"userAgreement\":\"By agreeing to create this connection, I give Merative permission to request my data from Health Insights, credentialize it, and send it to my digital wallet.\",\"website\":\"com/\"},\"proof\":{\"created\":\"2022-07-27T10:24:48Z\",\"creator\":\"did\",\"nonce\":\"c6cd9a75-e8d2-468d-8305-427667ea778f\",\"signatureValue\":\"MEQCIAQd94Q4zE7OXLYdpWUW_eMBBtPBLEQGJ4DK8uNIJMxgAiBA3FGtmy67qwcpVyAdx3GqCa2X7LoCa5StR4G_EpIYRg\",\"type\":\"EcdsaSecp256r1Signature2019\"}},{\"@context\":[\"v1\"],\"id\":\"did\",\"type\":[\"VerifiableCredential\",\"ID\"],\"issuer\":\"did\",\"issuanceDate\":\"2022-07-27T10:24:48Z\",\"expirationDate\":\"2023-07-27T10:24:48Z\",\"credentialSchema\":{\"id\":\"did;version=0.2\",\"type\":\"JsonSchemaValidator2018\"},\"credentialSubject\":{\"birthDate\":\"1965-02-05\",\"email\":\"[email protected]\",\"gender\":\"M\",\"id\":\"DOQQ163002\",\"name\":{\"familyName\":\"Ireson\",\"givenName\":\"Davian\"},\"organization\":\"fedex\",\"ssn\":\"123456789\",\"type\":\"id\"},\"proof\":{\"created\":\"2022-07-27T10:24:48Z\",\"creator\":\"did\",\"nonce\":\"19766c23-5826-4226-98b3-8680db73cd09\",\"signatureValue\":\"MEUCIBdmLk3Xh\",\"type\":\"EcdsaSecp256r1Signature2019\"}}]"

I have tried following approach but it's not working for me.

val myModelList: List<Model> = gson.fromJson(s.toJsonElement(), mutableListOf<Model>().javaClass)

Can anyone please let me know how I can convert this string to Json and then into List?

CodePudding user response:

The returned json format seems to be incorrect. Photo 1

CodePudding user response:

It is very simple is kotlin, No need to replace anything : Follow mentioned code :

val t = "[{\"@context\":[\"v1\"],\"id\":\"did\",\"type\":[\"VerifiableCredential\"],\"issuer\":\"did\",\"issuanceDate\":\"2022-07-27T10:24:48Z\",\"expirationDate\":\"2023-07-27T10:24:48Z\",\"credentialSchema\":{\"id\":\"didversion=0.2\",\"type\":\"JsonSchemaValidator2018\"},\"credentialSubject\":{\"contact\":\"[email protected]\",\"name\":\"Fedex\",\"orgId\":\"fedex\",\"privacyPolicy\":\"privacy-policy\",\"services\":[{\"purposes\":[{\"purpose\":\"To get my Health Insights data into my wallet.\"}],\"service\":\"download\"}],\"technical\":{\"download\":{\"id\":\"DOQQ163002\",\"linkId\":\"c97b8411-a6ba-4955-88e7-071353fbc2b8\",\"passcode\":\"2a57c0cd-b036-4c6f-9a41-adde8be8cc3e\",\"url\":\"attachments\"},\"symmetricKey\":{\"algorithm\":\"aes-256-cbc\",\"iv\":\"8qbl7cgPpuVzGrUSmt/Vsw==\",\"value\":\"3ROXMpHAEO8FNSVMzkTZIjnbFE7lD4T1FgwWs3B83JA=\"}},\"type\":\"profile\",\"userAgreement\":\"By agreeing to create this connection, I give Merative permission to request my data from Health Insights, credentialize it, and send it to my digital wallet.\",\"website\":\"com/\"},\"proof\":{\"created\":\"2022-07-27T10:24:48Z\",\"creator\":\"did\",\"nonce\":\"c6cd9a75-e8d2-468d-8305-427667ea778f\",\"signatureValue\":\"MEQCIAQd94Q4zE7OXLYdpWUW_eMBBtPBLEQGJ4DK8uNIJMxgAiBA3FGtmy67qwcpVyAdx3GqCa2X7LoCa5StR4G_EpIYRg\",\"type\":\"EcdsaSecp256r1Signature2019\"}},{\"@context\":[\"v1\"],\"id\":\"did\",\"type\":[\"VerifiableCredential\",\"ID\"],\"issuer\":\"did\",\"issuanceDate\":\"2022-07-27T10:24:48Z\",\"expirationDate\":\"2023-07-27T10:24:48Z\",\"credentialSchema\":{\"id\":\"did;version=0.2\",\"type\":\"JsonSchemaValidator2018\"},\"credentialSubject\":{\"birthDate\":\"1965-02-05\",\"email\":\"[email protected]\",\"gender\":\"M\",\"id\":\"DOQQ163002\",\"name\":{\"familyName\":\"Ireson\",\"givenName\":\"Davian\"},\"organization\":\"fedex\",\"ssn\":\"123456789\",\"type\":\"id\"},\"proof\":{\"created\":\"2022-07-27T10:24:48Z\",\"creator\":\"did\",\"nonce\":\"19766c23-5826-4226-98b3-8680db73cd09\",\"signatureValue\":\"MEUCIBdmLk3Xh\",\"type\":\"EcdsaSecp256r1Signature2019\"}}]";
val a = JSONArray(t)
Log.d(TAG, "onCreate: KISHAN  : $a")

Image From Android Studio

Converted JSON

  • Related