我是靠谱客的博主 懦弱世界,最近开发中收集的这篇文章主要介绍java json 枚举_Java枚举 - fastjson对enum的处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目的

1、枚举值转换成完全的json;

2、对象中的枚举成员完全转换成json;

3、枚举类的全部值转换成json;

枚举定义

48304ba5e6f9fe08f3fa1abda7d326ab.png

public enum SongsEnum {

SAFE_AND_SOUND(1,"Taylor Swift","Safe&Sound","2011-12-26")

,SHAKE_IT_OFF(2,"Taylor Swift","Shake It Off","2014-08-19")

,STYLE(3,"Taylor Swift","Style","2015-02-09")

,SOUND_OF_SILENCE(4,"Simon & Garfunkel","The Sound Of Silence","1966-01-17")

,BETTER_MAN(5,"Little Big Town","Better Man","2016-10-20")

,YESTERDAY_ONCE_MORE(6,"Carpenters","Yesterday Once More","1973-05-16")

;

public final int index;

public final String singer;

public final String name;

public final String date;

private SongsEnum(int seq, String singer, String name, String date) {

this.index = seq;

this.singer = singer;

this.name = name;

this.date = date;

}

public int getIndex() { return index; }

public String getSinger() { return singer; }

public String getName() { return name; }

public String getDate() { return date; }

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

一、枚举值转换json

期望结果:SongsEnum.SAFE_AND_SOUND -> {"date":"2011-12-26","index":1,"name":"Safe&Sound","singer":"Taylor Swift"}

## 默认调用结果

JSON.toJSONString(SongsEnum.SAFE_AND_SOUND) -> "SAFE_AND_SOUND"

JSON.toJSONString(SongsEnum.BETTER_MAN,SerializerFeature.WriteEnumUsingName) -> "BETTER_MAN"

JSON.toJSONString(SongsEnum.STYLE,SerializerFeature.WriteEnumUsingToString) -> "STYLE"

默认的使用fastjson转换enum,那么得到的enum json可能不是想要的.

(1) 重写enum的toString()

@Override

public String toString() {

return "{'name':"+this.name+",'singer':"+this.singer+"}";

}

JSON.toJSONString(SongsEnum.STYLE,SerializerFeature.WriteEnumUsingToString) -> "{'name':Style,'singer':Taylor Swift}"

虽然可以这样得到想要的结果,但相对来说太麻烦,每个enum类都要从写toString().

(此种方式并没有研究,所以以上重写toString()的代码可能存在问题)

(2) fastjson的config设置

fastjson提供的JSON.toJSONString(...)有很多重载的方法,例如:

public staticString toJSONString(Object object, SerializeConfig config, SerializerFeature... features)

所以,fastjson可以通过设置SerializeConfig来配置enum的序列化。

48304ba5e6f9fe08f3fa1abda7d326ab.png

public static void main(String[] args) {

SerializeConfig config = new SerializeConfig();

config.configEnumAsJavaBean(SongsEnum.class);

String s = JSON.toJSONString(SongsEnum.SOUND_OF_SILENCE, config);

System.out.println(s);

// {"date":"1966-01-17","index":4,"name":"The Sound Of Silence","singer":"Simon & Garfunkel"}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

二、对象中的枚举成员完全转换成json

48304ba5e6f9fe08f3fa1abda7d326ab.png

public enum StatusEnum {

STATUS_A(0,"状态A"),

STATUS_B(1,"状态B"),

STATUS_C(2,"状态C");

public final int index;

public final String status;

StatusEnum(int i, String status) {

this.index = i;

this.status = status;

}

public int getIndex() { return index; }

public String getStatus() { return status; }

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

class JavaBean{

private String name;

private SongsEnum song;

private StatusEnum status;

public JavaBean(String name,SongsEnum song,StatusEnum status){

this.name = name;

this.song = song;

this.status = status;

}

//省略setter/getter

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

期望: {"name":"vegilyn","song":{"date":"2014-08-19","index":2,"name":"Shake It Off","singer":"Taylor Swift"},"status":{"index":1,"status":"状态B"}}

默认结果:

JSON.toJSONString(new JavaBean("vegilyn",SongsEnum.SHAKE_IT_OFF,StatusEnum.STATUS_B)) -> {"name":"vegilyn","song":"SHAKE_IT_OFF","status":"STATUS_B"}

可以看出此结果和目的1中的结果一样的,所以通过目的1的方式也可以解决。

SerializeConfig config = new SerializeConfig();

config.configEnumAsJavaBean(SongsEnum.class); // 配置enum转换

String s = JSON

最后

以上就是懦弱世界为你收集整理的java json 枚举_Java枚举 - fastjson对enum的处理的全部内容,希望文章能够帮你解决java json 枚举_Java枚举 - fastjson对enum的处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部