zoukankan      html  css  js  c++  java
  • JSON类库 Flexjson学习

    官方地址(需FQ):http://flexjson.sourceforge.net/

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

    下面是我写的一个例子:http://download.csdn.net/detail/zzhd_xingfu/5885183

    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 args
    14      */
    15     public static void main(String[] args) {
    16         testJsonUtil();
    17     }
    18 
    19     public static void testJsonUtil() {
    20         // Json String to Java object
    21         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 简单的Object
    27         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 复杂的Object
    34         UserModel um3 = new UserModel();
    35         um3.setUserName("admin");
    36         um3.setPassWord("123");
    37         List<InterestModel> ims = new ArrayList<InterestModel>();
    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 String
    54         UserModel um4_1 = new UserModel();
    55         um4_1.setUserName("admin_1");
    56         um4_1.setPassWord("123_1");
    57         List<InterestModel> ims4_1 = new ArrayList<InterestModel>();
    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<InterestModel> ims4_2 = new ArrayList<InterestModel>();
    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<Object> list = new ArrayList<Object>();
    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 String
    11     public static String getJsonStrFromObj(Object obj) {
    12         JSONSerializer serializer = new JSONSerializer();
    13         return serializer.serialize(obj);
    14     }
    15 
    16     // Java object to JSON String
    17     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 String
    24     public static String getJsonStrFromList(List<Object> 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 object
    33     public static <T> T getObjFromJsonStr(String source, Class<T> bean) {
    34         return new JSONDeserializer<T>().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 Administrator
    10  * 
    11  */
    12 public class UserModel {
    13     private String userName;// 用户名
    14     private String passWord;// 密码
    15     private List<InterestModel> 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<InterestModel> getIms() {
    35         return ims;
    36     }
    37 
    38     public void setIms(List<InterestModel> 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     @Override
    51     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 }
  • 相关阅读:
    ios awakeFromNib 和 initWithCoder:
    iOS 关于iphone6 和 iphone6 plus 的适配
    iOS 目录的使用
    iOS 8 WKWebView 知识点
    iOS 动画结束后 view的位置 待完善
    iOS coredata 数据库升级 时报Can't find model for source store
    iOS 真机文件系统区分大小写,而模拟器可能不区分
    iOS coredata 级联删除
    iOS 关于AFNetworking ssl 待完成
    iOS 关于UIWindow的理解
  • 原文地址:https://www.cnblogs.com/xingfuzzhd/p/3241058.html
Copyright © 2011-2022 走看看