-4

I am getting a JSONObject in response from an API

{
"contact": null,
"error": false,
"msg": "get Contacts successfully",
"synccontactstimestamp": "2018-02-07 11:34:40.0",
"contactjson": "[{\"contactName\":\"4B9B4474C091D55A981ADBB7893FB4E1\",\"contactNumber\":\"0F8F3E9514D329F32CE7B03B9D276D72\"}, {\"contactName\":\"12E09C05C0E456AC2398BDA841986609\",\"contactNumber\":\"035774DB94BFE24BDEDA0B4B45F1372A0CC8EE72A457C3AAA35F79627FD13728\"}, {\"contactName\":\"9344BBF08ACACC4C85A9B83C55BA93C0\",\"contactNumber\":\"E1633601A6632356DB2F08058CA42504\"}, {\"contactName\":\"CCF51D9C27123ACD19F163F6C2BCCD28\",\"contactNumber\":\"89DD6C0E185D1067AC45CEE12DFA6A07\"}, {\"contactName\":\"A8309A8EC81E4DF50306FE4651D63F53\",\"contactNumber\":\"E8781EE87E9036B794BE44D7411FBFCF\"}

I am trying to get this JSONArray with key "contactjson" but it gives an exception Value of string type cannot be converted to JSONArray.

Any idea how to fetch this?

Neemani
  • 1
  • 4

1 Answers1

1

as you see value of contactjson key is coming as String,

First take your contactjson into a String variable like

 String data = yourResponse.contactjson;

then convert into JSONArray

 JSONArray jsonArr = new JSONArray(data);
Sachin
  • 4,110
  • 2
  • 16
  • 27
  • Is this going to work ? – ADM Feb 07 '18 at 12:13
  • yes it will convert the string to a JSONArray. – Sachin Feb 07 '18 at 12:14
  • The data is malformed itself . it need to be in proper formate. No need to create `JSONArray` from string when you can directly access it . – ADM Feb 07 '18 at 12:16
  • if you see value of contactjson key is coming as String, and then its throwing error Value of string type cannot be converted to JSONArray. we can convert it on our end what is wrong with this – Sachin Feb 07 '18 at 12:17
  • here question is not about should i change my data from the server , as User is getting in the app String as response , why can not we convert it and use :) – Sachin Feb 07 '18 at 12:18
  • 1
    thanks @Thunder..its working. You saved my day – Neemani Feb 07 '18 at 12:18
  • your welcome , please accept my answer Neemani – Sachin Feb 08 '18 at 16:50