博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON类库 Flexjson学习
阅读量:6115 次
发布时间:2019-06-21

本文共 6295 字,大约阅读时间需要 20 分钟。

官方地址(需FQ):

Flexjson 是一个将 Java 对象转成 JSON 的 类库,是一个深度转换的过程。

下面是我写的一个例子:

Main.java

1 package com.flexjson.main; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 import com.flexjson.model.InterestModel; 7 import com.flexjson.model.TelModel; 8 import com.flexjson.model.UserModel; 9 10 public class Main {11 12     /**13      * @param args14      */15     public static void main(String[] args) {16         testJsonUtil();17     }18 19     public static void testJsonUtil() {20         // Json String to Java object21         String json1 = "{\"ims\":[{\"name\":\"篮球\"},{\"name\":\"足球\"}],\"tm\":{\"number\":\"15896352635\",\"type\":\"手机号\"},\"passWord\":\"123\",\"userName\":\"admin\"}";22         UserModel um1 = JsonUtil.getObjFromJsonStr(json1, UserModel.class);23         System.out.println(um1.toString());24         // ///25 26         // Java object to JSON String 简单的Object27         UserModel um2 = new UserModel();28         um2.setUserName("admin");29         um2.setPassWord("123");30         System.out.println(JsonUtil.getJsonStrFromObj(um2));31         // ///32 33         // Java object to JSON String 复杂的Object34         UserModel um3 = new UserModel();35         um3.setUserName("admin");36         um3.setPassWord("123");37         List
ims = new ArrayList
();38 InterestModel im1 = new InterestModel();39 im1.setName("篮球");40 InterestModel im2 = new InterestModel();41 im2.setName("足球");42 ims.add(im1);43 ims.add(im2);44 um3.setIms(ims);45 TelModel tm = new TelModel();46 tm.setNumber("15896352635");47 tm.setType("手机号");48 um3.setTm(tm);49 String json3 = JsonUtil.getJsonStrFromObj(um3, "ims", "tm");50 System.out.println(json3);51 // ///52 53 // List to JSON String54 UserModel um4_1 = new UserModel();55 um4_1.setUserName("admin_1");56 um4_1.setPassWord("123_1");57 List
ims4_1 = new ArrayList
();58 InterestModel im4_1_1 = new InterestModel();59 im4_1_1.setName("篮球_1");60 InterestModel im4_1_2 = new InterestModel();61 im4_1_2.setName("足球_1");62 ims4_1.add(im4_1_1);63 ims4_1.add(im4_1_2);64 um4_1.setIms(ims4_1);65 TelModel tm4_1 = new TelModel();66 tm4_1.setNumber("15896352635_1");67 tm4_1.setType("手机号_1");68 um4_1.setTm(tm4_1);69 70 UserModel um4_2 = new UserModel();71 um4_2.setUserName("admin_2");72 um4_2.setPassWord("123_2");73 List
ims4_2 = new ArrayList
();74 InterestModel im4_2_1 = new InterestModel();75 im4_2_1.setName("篮球_2");76 InterestModel im4_2_2 = new InterestModel();77 im4_2_2.setName("足球_2");78 ims4_2.add(im4_2_1);79 ims4_2.add(im4_2_2);80 um4_2.setIms(ims4_2);81 TelModel tm4_2 = new TelModel();82 tm4_2.setNumber("15896352635_2");83 tm4_2.setType("手机号_2");84 um4_2.setTm(tm4_2);85 86 List
list = new ArrayList();87 list.add(um4_1);88 list.add(um4_2);89 System.out.println(JsonUtil.getJsonStrFromList(list, "User", "ims", "tm"));90 // ///91 92 }93 }

JsonUtil.java

1 package com.flexjson.main; 2  3 import java.util.List; 4  5 import flexjson.JSONDeserializer; 6 import flexjson.JSONSerializer; 7  8 public class JsonUtil { 9 10     // Java object to JSON String11     public static String getJsonStrFromObj(Object obj) {12         JSONSerializer serializer = new JSONSerializer();13         return serializer.serialize(obj);14     }15 16     // Java object to JSON String17     public static String getJsonStrFromObj(Object obj, String... params) {18         JSONSerializer serializer = new JSONSerializer();19         serializer = serializer.include(params);20         return serializer.serialize(obj);21     }22 23     // List to JSON String24     public static String getJsonStrFromList(List objs, String rootName,25             String... params) {26         JSONSerializer serializer = new JSONSerializer();27         serializer = serializer.include(params);28         serializer.rootName(rootName);29         return serializer.serialize(objs);30     }31 32     // Json String to Java object33     public static 
T getObjFromJsonStr(String source, Class
bean) {34 return new JSONDeserializer
().deserialize(source, bean);35 }36 37 38 }

UserModel.java

1 /** 2  *  3  */ 4 package com.flexjson.model; 5  6 import java.util.List; 7  8 /** 9  * @author Administrator10  * 11  */12 public class UserModel {13     private String userName;// 用户名14     private String passWord;// 密码15     private List
ims;16 private TelModel tm;17 18 public String getUserName() {19 return userName;20 }21 22 public void setUserName(String userName) {23 this.userName = userName;24 }25 26 public String getPassWord() {27 return passWord;28 }29 30 public void setPassWord(String passWord) {31 this.passWord = passWord;32 }33 34 public List
getIms() {35 return ims;36 }37 38 public void setIms(List
ims) {39 this.ims = ims;40 }41 42 public TelModel getTm() {43 return tm;44 }45 46 public void setTm(TelModel tm) {47 this.tm = tm;48 }49 50 @Override51 public String toString() {52 return "userName=" + userName + "; passWord=" + passWord + ";";53 }54 55 }

TelModel.java

1 package com.flexjson.model; 2  3 public class TelModel { 4     private String number; 5     private String type; 6  7     public String getType() { 8         return type; 9     }10 11     public void setType(String type) {12         this.type = type;13     }14 15     public String getNumber() {16         return number;17     }18 19     public void setNumber(String number) {20         this.number = number;21     }22 23 }

InterestModel.java

1 package com.flexjson.model; 2  3 public class InterestModel { 4     private String name; 5  6     public String getName() { 7         return name; 8     } 9 10     public void setName(String name) {11         this.name = name;12     }13 }

 

 

转载地址:http://gxvka.baihongyu.com/

你可能感兴趣的文章
一个很好的幻灯片效果的jquery插件--kinMaxShow
查看>>
微信支付签名配置正确,但返回-1,调不出支付界面(有的手机能调起,有的不能)...
查看>>
第二周例行报告
查看>>
多线程条件
查看>>
黄聪:VMware安装Ubuntu10.10【图解】转
查看>>
Centos 6.x 升级openssh版本
查看>>
公式推♂倒题
查看>>
vue实现点击展开,点击收起
查看>>
如何使frame能居中显示
查看>>
第k小数
查看>>
构建之法阅读笔记三
查看>>
写给对前途迷茫的朋友:五句话定会改变你的人生
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
JAVA入门到精通-第86讲-半双工/全双工
查看>>
bulk
查看>>
js document.activeElement 获得焦点的元素
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
day6-if,while,for的快速掌握
查看>>
JavaWeb学习笔记(十四)--JSP语法
查看>>