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
You can use this solution:
//org.json.JSONObject
JSONObject jObj = new JSONObject();
//com.google.gson.JsonObject
JsonObject jObj1 = new JsonObject();
jObj1.addProperty("jObj11", "value11");
JsonObject jObj2 = new JsonObject();
jObj2.addProperty("jObj12", "value12");
//JSONObject put(String name, Object value)
jObj.put("jObj1", jObj1);
jObj.put("jObj2", jObj2);
//Result
objectToJson(toMap(jObj));
public Map toMap(JSONObject content) {
final Map map = new HashMap<>(content.length());
for (final Iterator iterator = content.keys(); iterator.hasNext(); ) {
final String key = iterator.next();
map.put(key, get(key));
}
return map;
}
public static String objectToJson(T object) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(object);
}