Convert class into a JSONObject
Solution 1
I found that the following works with GSON:
User = new User();
Gson gson = new Gson();
String jsonString = gson.toJson(user);
try {
JSONObject request = new JSONObject(jsonString);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is not type safe, however.
Solution 2
Here is a crude example you can use to use Reflection to build the JSONObject..
Warning it's not pretty and does not contain really type-safety.
public static JSONObject quickParse(Object obj) throws IllegalArgumentException, IllegalAccessException, JSONException{
JSONObject object = new JSONObject();
Class<?> objClass = obj.getClass();
Field[] fields = objClass.getDeclaredFields();
for(Field field : fields) {
field.setAccessible(true);
Annotation[] annotations = field.getDeclaredAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof SerializedName){
SerializedName myAnnotation = (SerializedName) annotation;
String name = myAnnotation.value();
Object value = field.get(obj);
if(value == null)
value = new String("");
object.put(name, value);
}
}
}
return object;
}
Here is an example usage:
User user = new User();
JSONObject obj = quickParse(user);
System.out.println(obj.toString(3));
Output
{
"id": "",
"name": "",
"email": ""
}
TTransmit
Updated on November 03, 2020Comments
-
TTransmit over 3 years
I have several classes like this. I want to convert the classes into JSONObject format.
import java.io.Serializable; import com.google.gson.annotations.SerializedName; public class User implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("id") private Integer mId; @SerializedName("name") private String mName = ""; @SerializedName("email") private String mEmail; public Integer getId() { return mId; } public void setId(Integer id) { mId = id; } public String getName() { return mName; } public void setName(String name) { mName = name; } public String getEmail() { return mEmail; } public void setEmail(String email) { mEmail = email; } }
I know that I can convert these classes to JSONObject format as follows:
User user = new User(); JSONObject jsonObj = new JSONObject(); try { jsonObj.put("id", user.getId()); jsonObj.put("name", user.getName()); jsonObj.put("email", user.getEmail()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
The problem is that I need to do this for a lot of different classes that are much longer than this across a lot of files. Can I use GSON to fill the JSONObject from myClass so that I don't need to edit every time the class structure changes?
The following returns a JSON string but I need it as an Object as when I send it to the system that sends the requests via a REST API it sends with unwanted quotation marks.
User user = new User(); Gson gson = new Gson(); Object request = gson.toJson(user);
When I use this in another JSON builder that asks for an Object I get
{"request":"{"id":"100","name":"Test Name","email":"[email protected]"}"}
When I want
{"request":{"id":"100","name":"Test Name","email":"[email protected]"}}