Java 将类转换为JSONObject
我有好几节这样的课。我想将这些类转换为JSONObject格式Java 将类转换为JSONObject,java,json,gson,jsonobject,Java,Json,Gson,Jsonobject,我有好几节这样的课。我想将这些类转换为JSONObject格式 import java.io.Serializable; import com.google.gson.annotations.SerializedName; public class User implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("id") private I
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;
}
}
我知道我可以将这些类转换为JSONObject格式,如下所示:
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();
}
问题是,我需要对许多不同的类执行此操作,这些类在许多文件中都比这个长得多。我是否可以使用GSON从myClass填充JSONObject,这样我就不必每次更改类结构时都进行编辑
下面返回一个JSON字符串,但我需要它作为一个对象,就像我将它发送到系统时一样,该系统通过RESTAPI发送请求,该系统使用不需要的引号
User user = new User();
Gson gson = new Gson();
Object request = gson.toJson(user);
当我在另一个JSON构建器中使用它时,它会请求我得到的对象
{"request":"{"id":"100","name":"Test Name","email":"test@example.com"}"}
当我想
{"request":{"id":"100","name":"Test Name","email":"test@example.com"}}
下面是一个粗略的示例,您可以使用反射来构建JSONObject 警告:它不美观,不包含真正的类型安全性
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;
}
输出
{
"id": "",
"name": "",
"email": ""
}
我发现以下内容适用于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();
}
但是,这不是类型安全的 尝试使用以下代码:
// Returns the JSON in a String
public String getJSON()
{
Gson gson = new Gson();
return gson.toJson(this);
}
// Builds the Model Object from the JSON String
MyModel model =new MyModel();
JSONObject j = new JSONObject(model.getJSON());
请注意,
JSONObject
来自org.json
依赖性。不是从GSON
获取此错误时,构造函数JSONObject(字符串)未定义。
// Returns the JSON in a String
public String getJSON()
{
Gson gson = new Gson();
return gson.toJson(this);
}
// Builds the Model Object from the JSON String
MyModel model =new MyModel();
JSONObject j = new JSONObject(model.getJSON());