0

Why does my Json parse fail with following error?

JSON from Web API

[{"CUSTJOBID":1,"JOBDT":"2020-11-18T00:00:00","JobFromAdd":"TT4 6PX","JobToAdd":"GH8 7HH"}]

KOTLIN CODE to PASRSE

val jsonarray = JSONArray(response.toString())

App error of type java.lang.String cannot be converted to JSONArray

JMP
  • 4,417
  • 17
  • 30
  • 41
  • The JSON that you posted is correct for JSONArray but can you show the whole response JSON? – Hayssam Soussi Nov 20 '20 at 13:03
  • HI Hayssam, when i use post man it comes as this "[{\"CUSTJOBID\":1,\"JOBDT\":\"2020-11-18T00:00:00\",\"JobFromAdd\":\"ST4 6PX\",\"JobToAdd\":\"B78 3HH\"}]" and error says Caused by: org.json.JSONException: Value [{"CUSTJOBID":1,"JOBDT":"2020-11-18T00:00:00","JobFromAdd":"ST4 6PX","JobToAdd":"B78 3HH"},{"CUSTJOBID":1,"JOBDT":"2020-11-18T00:00:00","JobFromAdd":"ST4 6PX","JobToAdd":"B78 3HH"}] of type java.lang.String cannot be converted to JSONArray – Crown CallStats Nov 20 '20 at 13:14
  • this is the whole json, as im converting a data table to json using a vb.net function on web api – Crown CallStats Nov 20 '20 at 13:17
  • If the opening and closing quotes `"[...]"` are included as part of the response that could be it – cutiko Nov 20 '20 at 13:38
  • var strres ="[{\"CUSTJOBID\":1,\"JOBDT\":\"2020-11-18T00:00:00\",\"JobFromAdd\":\"TT4 6PX\",\"JobToAdd\":\"GH8 7HH\"}]" works and no errors going to Array but if i use api response and take out first and last charactors like var strres =response.toString() strres =strres.toString().substring(1, response.length-1) Log.e("Response", strres) val jsonarray = JSONArray(strres), it throws Caused by: org.json.JSONException: Expected literal value at character 2 of [{\"CUSTJOBID\":1,\"JOBDT – Crown CallStats Nov 20 '20 at 13:51
  • How can i use api response to format like static variable i used above, i have tried taking out first and last charactors but failed with Caused by: org.json.JSONException: Expected literal value at character 2 of [{\"CUSTJOBID\":1,\"JOBDT – Crown CallStats Nov 20 '20 at 14:47

0 Answers0