- @JSONField(format="yyyy-MM-ddHH:mm:ss")@H_502_21@
- publicDatebirthday;@H_502_21@
@JSONField (format="yyyy-MM-dd HH:mm:ss")
public Date birthday;
另一种是通过SerializeConfig:
- privatestaticSerializeConfigmapping=newSerializeConfig();@H_502_21@
- privatestaticStringdateFormat;@H_502_21@
- static{@H_502_21@
- dateFormat="yyyy-MM-ddHH:mm:ss";@H_502_21@
- mapping.put(Date.class,newSimpleDateFormatSerializer(dateFormat));@H_502_21@
- }@H_502_21@
private static SerializeConfig mapping = new SerializeConfig();
private static String dateFormat;
static {
dateFormat = "yyyy-MM-dd HH:mm:ss";
mapping.put(Date.class,new SimpleDateFormatSerializer(dateFormat));
}
json字符串中使用单引号:
String text = JSON.toJSONString(object,SerializerFeature.UseSingleQuotes);
字段显示不同的key:
- publicclassUser{@H_502_21@
- @JSONField(name="ID")@H_502_21@
- publicintgetId(){...}@H_502_21@
- }@H_502_21@
- @H_502_21@
- Useruser=...;@H_502_21@
- JSON.toJSONString(user);@H_502_21@
public class User {
@JSONField(name="ID")
public int getId() { ... }
}
User user = ...;
JSON.toJSONString(user); // {"ID":001}
类的反序列化 JavaBean:
- Stringtext=...;@H_502_21@
- Colorcolor=JSON.parSEObject(text,Color.class);@H_502_21@
String text = ...; // {"r":255,"alpha":255}
Color color = JSON.parSEObject(text,Color.class);
数组:
- Stringtext=...;@H_502_21@
- List<User>users=JSON.parseArray(text,User.class);@H_502_21@
String text = ...; // [{ ... },{ ... }]
List<User> users = JSON.parseArray(text,User.class);
泛型:
- Stringtext=...;@H_502_21@
- Map<String,User>userMap=JSON.parSEObject(text,newTypeReference<Map<String,User>>(){});@H_502_21@
String text = ...; // {"name":{"name":"ljw",age:18}}
Map<String,User> userMap = JSON.parSEObject(text,new TypeReference<Map<String,User>>() {});
自定义序列化代码示例:
- publicclassJsonUtil{@H_502_21@
- privatestaticSerializeConfigmapping=newSerializeConfig();@H_502_21@
- privatestaticStringdateFormat;@H_502_21@
- static{@H_502_21@
- dateFormat="yyyy-MM-ddHH:mm:ss";@H_502_21@
- }@H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- publicstaticStringtoJSON(ObjectjsonText){@H_502_21@
- returnJSON.toJSONString(jsonText,@H_502_21@
- SerializerFeature.WriteDateUseDateFormat);@H_502_21@
- }@H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- publicstaticStringtoJSON(StringdateFormat,StringjsonText){@H_502_21@
- mapping.put(Date.class,newSimpleDateFormatSerializer(dateFormat));@H_502_21@
- returnJSON.toJSONString(jsonText,mapping);@H_502_21@
- }@H_502_21@
- }@H_502_21@
public class JsonUtil {
private static SerializeConfig mapping = new SerializeConfig();
private static String dateFormat;
static {
dateFormat = "yyyy-MM-dd HH:mm:ss";
}
/**
* 默认的处理时间
*
* @param jsonText
* @return
*/
public static String toJSON(Object jsonText) {
return JSON.toJSONString(jsonText,SerializerFeature.WriteDateUseDateFormat);
}
/**
* 自定义时间格式
*
* @param jsonText
* @return
*/
public static String toJSON(String dateFormat,String jsonText) {
mapping.put(Date.class,new SimpleDateFormatSerializer(dateFormat));
return JSON.toJSONString(jsonText,mapping);
}
}
自定义反序列化示例:
先自定义一个日期解析类:
- publicclassMyDateFormatDeserializerextendsDateFormatDeserializer{@H_502_21@
- @H_502_21@
- privateStringmyFormat;@H_502_21@
- @H_502_21@
- publicMyDateFormatDeserializer(StringmyFormat){@H_502_21@
- super();@H_502_21@
- this.myFormat=myFormat;@H_502_21@
- }@H_502_21@
- @H_502_21@
- @Override@H_502_21@
- protected<Date>Datecast(DefaultJSONParserparser,Typeclazz,ObjectfieldName,Objectval){@H_502_21@
- if(myFormat==null){@H_502_21@
- returnnull;@H_502_21@
- }@H_502_21@
- if(valinstanceofString){@H_502_21@
- StringstrVal=(String)val;@H_502_21@
- if(strVal.length()==0){@H_502_21@
- returnnull;@H_502_21@
- }@H_502_21@
- @H_502_21@
- try{@H_502_21@
- return(Date)newSimpleDateFormat(myFormat).parse((String)val);@H_502_21@
- }catch(ParseExceptione){@H_502_21@
- thrownewJSONException("parseerror");@H_502_21@
- }@H_502_21@
- }@H_502_21@
- thrownewJSONException("parseerror");@H_502_21@
- }@H_502_21@
- }@H_502_21@
public class MyDateFormatDeserializer extends DateFormatDeserializer {
private String myFormat;
public MyDateFormatDeserializer(String myFormat) {
super();
this.myFormat = myFormat;
}
@Override
protected <Date> Date cast(DefaultJSONParser parser,Type clazz,Object fieldName,Object val) {
if (myFormat == null) {
return null;
}
if (val instanceof String) {
String strVal = (String) val;
if (strVal.length() == 0) {
return null;
}
try {
return (Date) new SimpleDateFormat(myFormat).parse((String)val);
} catch (ParseException e) {
throw new JSONException("parse error");
}
}
throw new JSONException("parse error");
}
}
- publicclassUser{@H_502_21@
- @H_502_21@
- publicStringname;@H_502_21@
- publicintheight;@H_502_21@
- @H_502_21@
- @JSONField(name="com-google-com")@H_502_21@
- publicvoidsetName(Stringname){@H_502_21@
- this.name=name;@H_502_21@
- }@H_502_21@
- @H_502_21@
- @JSONField(format="yyyy-MM/ddHH:mm:ss")@H_502_21@
- publicDatebirthday;@H_502_21@
- }@H_502_21@
public class User {
public String name;
public int height;
@JSONField(name = "com-google-com")
public void setName(String name) {
this.name = name;
}
@JSONField(format = "yyyy-MM/dd HH:mm:ss")
public Date birthday;
}
测试下:
- @H_502_21@
- @H_502_21@
- @H_502_21@
- @H_502_21@
- publicstaticvoidmain(String[]args)throwsIOException,ParseException{@H_502_21@
- @H_502_21@
- Stringjson="{\"name\":\"22323\",\"age\":1234,"+@H_502_21@
- "\"birthday\":\"2012-12/1212:12:12\"}";@H_502_21@
- Testt=JSON.parSEObject(json,Test.class,mapping,@H_502_21@
- JSON.DEFAULT_PARSER_FEATURE,newFeature[0]);@H_502_21@
- System.out.println(t.name);@H_502_21@
- System.out.println(t.height);@H_502_21@
- System.out.println(t.birthday);@H_502_21@
- System.out.println(@H_502_21@
- newSimpleDateFormat("yyyy-MM/ddHH:mm:ss").parse("2012-12/1212:12:12"));@H_502_21@
- }@H_502_21@
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException,ParseException {
String json = "{\"name\":\"22323\",\"age\": 1234," +
" \"birthday\": \"2012-12/12 12:12:12\"}";
Test t = JSON.parSEObject(json,Test.class,JSON.DEFAULT_PARSER_FEATURE,new Feature[0]);
System.out.println(t.name);
System.out.println(t.height);
System.out.println(t.birthday);
System.out.println(
new SimpleDateFormat("yyyy-MM/dd HH:mm:ss").parse("2012-12/12 12:12:12"));
}
总结:对于JSONField注解,好像只对序列号的格式有影响,反序列化不管这个,不知道为什么,只能自己写个解析类了,不过这样就更灵活了,可以在里面写很多处理逻辑,比如json字符串里面日期格式并不是标准格式的时候,就可以先转成标准格式再去解析了。
另外,fastjson的document官网一直打不开,不知道神马原因,难道被feng了麽:
原文链接:https://www.f2er.com/json/290207.html