I am trying to rebuild an Object
from its fields( i get the fields as a JSONObject), something like this:
JSONObject jObj = new JSONObject();
JSO
I had the same problem when I wanted to use Gson only for pretty print. JsonObject is working fine but if you still want to use JSONObject instead of JsonObject then you can use it that way:
public class JsonUtil {
public static String toPrettyFormat(String jsonString)
{
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
return prettyJson;
}
}
and simply pass JSONObject as string like this:
String prettyJson = JsonUtil.toPrettyFormat(jsonObject.toString());