我有以下代码,JSON和POJO,但即使JSON属性具有值,结果dataToUse对象的名称和日期也为null。有没有一种特殊的方法来处理json中的'@'?我有另一个完全类似的代码正确解析,但没有'@'。
DataToUse dataToUse = new Gson().fromJson(json.toString(), DataToUse.class);
JSON:
{“@name”: “A Name”,“@date”: "2017-12-11T18:00:00-05:00"}
POJO:
public class DataToUse {
private String name;
private Date date;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }
}
答案 0 :(得分:1)
通过添加@SerializedName注释
在下面尝试 public class DataToUse {
@SerializedName("@name")
private String name;
@SerializedName("@date")
private Date date;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }
}
答案 1 :(得分:0)
通常使用GSON,您需要在create gson进程中注册Date解析功能。我通常会创建一个名为JsonHelper的包装类来让我的生活更轻松。注意日期处理部分。但您可能会遇到@符号问题,但您可能还需要处理日期格式。
/**
* Created by App Studio 35 on 3/18/15.
*/
public class JSONHelper {
/*//////////////////////////////////////////////////////////
// MEMBERS
*///////////////////////////////////////////////////////////
private static Gson mGson; //Google's JSON to Object Converter
private static final String TAG = JSONHelper.class.getSimpleName();
/*//////////////////////////////////////////////////////////
// PROPERTIES
*///////////////////////////////////////////////////////////
protected static Gson getGson(){
return mGson == null ? mGson = getGsonBuilder().create() : mGson;
}
/*//////////////////////////////////////////////////////////
// METHODS
*///////////////////////////////////////////////////////////
private static GsonBuilder getGsonBuilder(){
//Used for Building a Gson Converter a Google Product for JSON Translation to Objects
GsonBuilder builder = new GsonBuilder();
//Created as Static so Fragments can easily use the same Builder.
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
//Specify the way we format Dates in this Converter
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.getDefault());
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = json.getAsJsonPrimitive().getAsString();
try {
return format.parse(date);
} catch (ParseException e) {
Log.e(TAG, "getGsonBuilder failed to register Date: " + e.getMessage());
throw new RuntimeException(e);
}
}
});
return builder;
}
/**
*
* @param objectToConvert to convert to Json
* @param classType the class that we are serializing from into a String
* @return
*/
public static String toJson(Object objectToConvert, Class classType){
try {
String msg = "";
//Get String from Gson converter for Class Type
msg = getGson().toJson(objectToConvert, classType);
//Return String of Json from passed in Object
return msg;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return "";
}
}
/**
*
* @param objectToConvert to convert to Json
* @param classType the Type that we are serializing from into a String
* @return
*/
public static String toJson(Object objectToConvert, Type classType){
try {
String msg = "";
//Get String from Gson converter for Class Type
msg = getGson().toJson(objectToConvert, classType);
//Return String of Json from passed in Object
return msg;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return "";
}
}
/**
*
* @param objectToConvert Object to convert to JSON string
* @return json string to represent the passed object
*/
public static String toJson(Object objectToConvert){
try {
String msg = "";
//Get String from Gson converter for Class Type
msg = getGson().toJson(objectToConvert);
//Return String of Json from passed in Object
return msg;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return "";
}
}
/**
*
* @param json String to convert into Object for the class that was passed
* @param classType the class that we are serializing from
* @return
*/
public static Object fromJson(String json, Class classType){
try {
//Get Object from Gson converter of Json
Object obj = getGson().fromJson(json, classType);
//Return filled in object of passed in Class Type
return obj;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return null;
}
}
/**
*
* @param json String to convert into Json for the class that was passed
* @param classType the Type that we are serializing from
* @return
*/
public static Object fromJson(String json, Type classType){
try {
//Get Object from Gson converter of Json
Object obj = getGson().fromJson(json, classType);
//Return filled in object of passed in Class Type
return obj;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return null;
}
}
/**
*
* @param jsonElement JsonElement to convert into Object for the class that was passed
* @param classType the class that we are serializing from
* @return
*/
public static Object fromJson(JsonElement jsonElement, Class classType){
try {
//Get Object from Gson converter of Json
Object obj = getGson().fromJson(jsonElement, classType);
//Return filled in object of passed in Class Type
return obj;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return null;
}
}
/**
*
* @param jsonElement String to convert into Object for the class that was passed
* @param classType the class that we are serializing from
* @return
*/
public static Object fromJson(JsonElement jsonElement, Type classType){
try {
//Get Object from Gson converter of Json
Object obj = getGson().fromJson(jsonElement, classType);
//Return filled in object of passed in Class Type
return obj;
} catch (Exception e) {
Log.e(TAG, e.getMessage() == null ? "null" : e.getMessage());
return null;
}
}
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
}
答案 2 :(得分:-1)
您应该从参数中删除@
{“@name”: “A Name”,“@date”: "2017-12-11T18:00:00-05:00"}
{“name”: “A Name”,“date”: "2017-12-11T18:00:00-05:00"}
尝试此解决方案。