com.fasterxml.jackson 可以忽略大小写,类内部可以使用List
public static
//Json字符串转为java对象
ObjectMapper ob =new ObjectMapper();
//忽略未定义字段
ob.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//json转bean时忽略大小写
ob.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
T bean = ob.readValue(jsonString, beanClass);
return bean;
}
public static
//Json字符串转为java对象
ObjectMapper ob =new ObjectMapper();
//忽略未定义字段
ob.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//json转bean时忽略大小写
ob.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
List>() {});
return list;
}
gson,不能直接忽略大小写,可以通过 @SerializedName(“custom_naming”) private final String someField; 或者UpperCamelCase
内部有List时可以用
public static
Gson gson = new Gson();
T bean = gson.fromJson(jsonString, beanClass);
return bean;
}
public static
Gson gson = new Gson();
Type type = new ParameterizedTypeImpl(clazz);
List
return list;
}
public class ParameterizedTypeImpl implements ParameterizedType {
Class clazz;
public ParameterizedTypeImpl(Class clz) {
clazz = clz;
}
public Type[] getActualTypeArguments() {
return new Type[]{clazz};
}
public Type getRawType() {
return List.class;
}
public Type getOwnerType() {
return null;
}
}
net.sf.json 不能忽略大小写,而且类内部有List时,序列化有问题,需要借助单独定义Map参数解决
public static
JSONArray jsonArray = JSONArray.fromObject(jsonString);
JSONObject jsonObject;
T bean;
int size = jsonArray.size();
List
for (int i = 0; i < size; i++) {
jsonObject = jsonArray.getJSONObject(i);
bean = (T) JSONObject.toBean(jsonObject, beanClass);
list.add(bean);
}
return list;
}
/*
从一个JSON 对象字符格式中得到一个java对象
@param jsonString
@param beanCalss
@return
*/
@SuppressWarnings(“unchecked”)
public static
JSONObject jsonObject = JSONObject.fromObject(jsonString);
T bean = (T) JSONObject.toBean(jsonObject, beanCalss);
return bean;
}