Android : gson.toJson() doesn't give standard json

195 Views Asked by At

When i tried to convert Object to json format using gson.toJson(myObject) it gives me not standard json format

Output currently : {\"objects\":[],\"type\":\"price\"}

what should i need is like this : {"objects":[],"type":"price"}

below how i convert my Object to json :

Gson gson = new Gson();
                String dataJson = gson.toJson(MyObject);
                System.out.println("LOG : " + dataJson);

My question is : How to make normal json format using Gson? thanks.

1

There are 1 best solutions below

0
Mohit Trivedi On

Hi R Besar,

        String json="{\\\"objects\\\":[],\\\"type\\\":\\\"price\\\"}";
        Log.d("TAG","Old Json: "+json);

        String newJson=json.replace("\\","");
        Log.d("TAG","New Json: "+newJson);

OUTPUT

{\"objects\":[],\"type\":\"price\"}
{"objects":[],"type":"price"}

I think this will help you