利用fastjson反序列化json为对象和对象数组
利用fastjson反序列化json为对象和对象数组
利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
【主要特点】
···快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)
···强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
···零依赖(没有依赖其它任何类库除了JDK) —— [ 开源中国社区]
反序列化函数
· parseObject
-将 json字符串 反序列化为 java.class
@sherineZJU List<T> temp= JSON.parseObject(fastjson,T.class); //transform the json to T 1 2 3
-fastjson 为 json格式的字符串(String类型)
-T 为 与 json格式对应的 自定义 java.class。
· parseObject
-将 json字符串 反序列化为 java.util.List
@sherineZJU List<T> oldk = JSONArray.parseArray(fastjson,T.class); //transform the json to List<T> 1 2 3
-fastjson 为 json格式的字符串(String类型)
-List< T> 为 与 json格式对应的 自定义 java.util.List。
java.class 与 json字符串 的对应关系
注意 json字符串 的关键字一定要与 java.class 中的对应变量名相同。
- 第一个例子
json字符串
{ "version": [ { "intro": "断除堕胎,杀人,杀牛,宰羊,杀猪,宰鸡,杀大象,杀马,杀鱼,打死蚊子、螞蟻、蟑螂等,杀龙、猴子,出佛身血,自杀。", "degree": 1, "word": "不杀生" }, { "intro": "堕胎,杀人,杀牛,宰羊,杀猪,宰鸡,杀大象,杀马,杀鱼,打死蚊子、螞蟻、蟑螂等,杀龙、猴子,出佛身血,自杀。伤害别人。自己亲自做,唆使别人做,赞叹别人做。用器杖、毒药、明咒等。", "degree": -1, "word": "杀生" } ], "name": "杀生", "id": "x-1-1", "style": "身三", "style_color": 1, "io": "i" } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
java.class
import java.util.ArrayList; import java.util.List; /** * @author sherine_zju * @version 1.0 * fastjson 将 json 反序列化 为 java.class and java.util.List */ public class old_karma { List<version> version = new ArrayList<version>(); public String name; public String id; public String style; public int style_color; public char io; public List<version> getVersion() { return version; } public void setVersion(List<version> version) { this.version = version; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public int getStyle_color() { return style_color; } public void setStyle_color(int style_color) { this.style_color = style_color; } public char getIo() { return io; } public void setIo(char io) { this.io = io; } } class version{ public String intro; public int degree; public String word; public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } public int getDegree() { return degree; } public void setDegree(int degree) { this.degree = degree; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
-第二个例子
json字符串
{ "bad": { "degree": -1, "intro": "堕胎,杀人,杀牛,宰羊,杀猪,宰鸡,杀大象,杀马,杀鱼,打死蚊子、螞蟻、蟑螂等,杀龙、猴子,出佛身血,自杀。伤害别人。自己亲自做,唆使别人做,赞叹别人做。用器杖、毒药、明咒等。", "name": "杀生", "word": "杀生" }, "good": { "degree": 1, "intro": "断除堕胎,杀人,杀牛,宰羊,杀猪,宰鸡,杀大象,杀马,杀鱼,打死蚊子、螞蟻、蟑螂等,杀龙、猴子,出佛身血,自杀。", "name": "不杀生", "word": "不杀生" }, "id": "x-1-1", "io": "i", "name": "杀生", "style": "身三", "style_color": 1 } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
java.class
import java.util.ArrayList; import java.util.List; /** * @author sherine_zju * @version 1.0 * fastjson 将 json 反序列化 为 java.class and java.util.List */ public class new_karma { public String name; public String id; public String style; public int style_color; public char io; public Bad bad; public Good good; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public int getStyle_color() { return style_color; } public void setStyle_color(int style_color) { this.style_color = style_color; } public char getIo() { return io; } public void setIo(char io) { this.io = io; } public Bad getBad() { return bad; } public void setBad(Bad bad) { this.bad = bad; } public Good getGood() { return good; } public void setGood(Good good) { this.good = good; } } class Good{ public String name; public int degree; public String word; public String intro; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDegree() { return degree; } public void setDegree(int degree) { this.degree = degree; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } } class Bad{ public String name; public int degree; public String word; public String intro; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDegree() { return degree; } public void setDegree(int degree) { this.degree = degree; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public String getIntro() { return intro; } public void setIntro(String intro) { this.intro = intro; } }
相关推荐
80337960 2020-06-10
ITprivate 2020-03-26
88483063 2020-06-28
88483063 2020-05-25
88103756 2020-05-02
88483063 2020-04-23
80337960 2020-03-26
80337960 2020-02-22
88483063 2020-01-29
83163452 2020-01-28
baijinswpu 2020-01-25
88483063 2020-01-11
86403969 2020-01-04
88103756 2020-01-01
88103756 2019-12-24
fengchao000 2019-12-24
80337960 2019-12-23
xufankang 2019-12-19
88483063 2019-12-16