0

This is my json string :

{
gateway=vary, 
gateway_text=xyz, 
gateway_data=, 
start=17/02/2022 06:23:45, 
end=17/03/2022 06:23:45, 
promo_time=17/03/2022 06:23:45, 
in_process_canceled=0.0, 
id=817957632
}

When I am trying to convert it to JSON object, it is throwing the exception

val obj = JSONObject(purchaseData.toString())

Please help me resolve this.

Nikitha
  • 49
  • 5

2 Answers2

1

Your json is not a valid json, remove "=" and put double quotes in property and values

{
    "gateway": "vary",
    "gateway_text": "xyz",
    "gateway_data": "ss",
    "start": "17/02/202206:23:45",
    "end": "17/03/202206:23:45",
    "promo_time": "17/03/202206:23:45",
    "in_process_canceled": "0.0",
    "id": "817957632"
}

Then it will work

Mostafa Hassan
  • 288
  • 1
  • 14
  • Is there any way to convert the same programatically – Nikitha Mar 14 '22 at 12:17
  • 1
    You should have a right json format, but incase if you don't have control over this output, a string replace can do the job, here you are an example: json = json.replace(" ","").replace("\n","").replace("{", "{\"").replace("}", "\"}").replace("=", "\":\"").replace(",", "\",\""); , please mark the question as answered if this works with you, thanks – Mostafa Hassan Mar 14 '22 at 16:03
0

This can handled by converting it to a json string ;


json = json.replace(" ","")
        json = json.replace("\n","")
        json = json.replace("{", "{\"")
        json = json.replace("}", "\"}")
        json = json.replace("=", "\":\"")
        json = json.replace(",", "\",\"")
val obj = JSONObject(json)

Nikitha
  • 49
  • 5