Skip to content

Instantly share code, notes, and snippets.

@jialechan
Last active February 26, 2016 06:14
Show Gist options
  • Save jialechan/794dd90697f90abadf8f to your computer and use it in GitHub Desktop.
Save jialechan/794dd90697f90abadf8f to your computer and use it in GitHub Desktop.
fastjson自定义反序列化
public class LoginResponse {
    private UserInfo userInfo;
    ...
}
public class UserInfo {
    private List<String> attrList;
    ...
}
public static void main(String[] args) {
    ExtraProcessor processor = new ExtraProcessor(){
        @Override
        public void processExtra(Object object, String key, Object value) {
            if("attr1".equals(key)) {
                UserInfo userInfo = (UserInfo)object;
                String strValue = (String)value;
                userInfo.setAttrList(Arrays.asList(strValue.split(",")));
            }
        }
    };
    String str = "{\"userInfo\":{\"attr1\":\"abc,efg,hij\"}}";
    LoginResponse loginResponse = JSON.parseObject(str, LoginResponse.class, processor);
    System.out.println(loginResponse);
}
LoginResponse{userInfo=UserInfo{attrList=[abc, efg, hij]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment