• JSON与Javabean转换的几种形式


        JSON格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与Javabean之间的转换:

      String json1="{'name':'zhangsan','age':23,'interests':[{'interest':'篮球','colors':['绿色','黄色']},{'interest':'足球','colors':['红色','蓝色']}]}";
      String json2="[{'name':'zhangsan'},{'name':'lisi'},{'name':'王五'}]";
      String json3="{'1':{'name':'zhangsan'},'3':{'name':'lisi'},'4':{'name':'wangwu'}}";//map
      String json4="{'name':'zhangsan','age':23}";

    首先,此处的转化依赖两个JAR包

     1     <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
     2     <dependency>
     3         <groupId>com.google.code.gson</groupId>
     4         <artifactId>gson</artifactId>
     5         <version>2.8.1</version>
     6     </dependency>
     7     <!-- https://mvnrepository.com/artifact/org.json/json -->
     8     <dependency>
     9         <groupId>org.json</groupId>
    10         <artifactId>json</artifactId>
    11         <version>20170516</version>
    12     </dependency>
    View Code

    其次,封装的Javabean代码如下

     1 import java.util.List;
     2 
     3 public class UserBean {
     4 
     5     private String name;
     6     
     7     private Integer age;
     8     
     9     private List<InterestBean> interests;
    10 
    11     public String getName() {
    12         return name;
    13     }
    14 
    15     public void setName(String name) {
    16         this.name = name;
    17     }
    18 
    19     public Integer getAge() {
    20         return age;
    21     }
    22 
    23     public void setAge(Integer age) {
    24         this.age = age;
    25     }
    26     
    27     
    28     
    29     
    30     public List<InterestBean> getInterests() {
    31         return interests;
    32     }
    33 
    34     public void setInterests(List<InterestBean> interests) {
    35         this.interests = interests;
    36     }
    37 
    38 
    39 
    40 
    41      class InterestBean{
    42         private String interest;
    43         
    44         private List<String> colors;
    45 
    46         public String getInterest() {
    47             return interest;
    48         }
    49 
    50         public void setInterest(String interest) {
    51             this.interest = interest;
    52         }
    53 
    54         public List<String> getColors() {
    55             return colors;
    56         }
    57 
    58         public void setColors(List<String> colors) {
    59             this.colors = colors;
    60         }
    61         
    62         
    63     }
    64     
    65 }
    View Code

    1、普通的json4格式的JSON解析:

     1 public void testParseJson(){
     2         
     3         JSONObject jsonObj = new JSONObject(json4);
     4         String name = jsonObj.getString("name");
     5         int age = jsonObj.getInt("age");
     6         System.out.println(name);
     7         System.out.println(age);
     8         UserBean user = new UserBean();
     9         user.setAge(age);
    10         user.setName(name);
    11         
    12     }
    View Code

    2、数组形式的JSON解析以及GSON解析:

    1 public void testJsonArray(){
    2         JSONArray jsonArray = new JSONArray(json2);
    3         for (int i = 0; i < jsonArray.length(); i++) {
    4             JSONObject jsonObj = jsonArray.getJSONObject(i);
    5             String name = jsonObj.getString("name");
    6             System.out.println(name);
    7 
    8         }
    9     }
    View Code
     1     /**
     2      * 解析json数组
     3      */
     4     public void testParseListJson(){
     5         Gson gson = new Gson();
     6         Type type = new TypeToken<List<UserBean>>(){}.getType();
     7         List<UserBean> users = gson.fromJson(json2, type);
     8         for(UserBean user:users){
     9             System.out.println(user.getName());
    10         }
    11     }
    View Code

    3、内嵌JSON形式的JSON与GSON解析:

     1 /**
     2      * 内嵌JSON解析
     3      */
     4     public void testParseJson1(){
     5         JSONObject rootJson = new JSONObject(json1);
     6         JSONArray jsonInterestArray = rootJson.getJSONArray("interests");
     7         for (int i = 0; i < jsonInterestArray.length(); i++) {
     8             JSONObject interestJsonObj = jsonInterestArray.getJSONObject(i);
     9             String interest = interestJsonObj.getString("interest");
    10             System.out.println(interest);
    11             Object obj = interestJsonObj.get("colors");
    12             System.out.println(obj);
    13         }
    14     }
    View Code
     1 /**
     2      * 内嵌GSON解析
     3      */
     4     public void testSimpleJson(){
     5         Gson gson = new Gson();
     6         UserBean user = gson.fromJson(json1, UserBean.class);
     7         System.out.println(user.getName());
     8         System.out.println(user.getAge());
     9         System.out.println(user.getInterests().size());
    10         List<InterestBean> list = user.getInterests();
    11         for(InterestBean bean:list) {
    12             System.out.println(bean.getInterest());
    13             List<String> colors = bean.getColors();
    14             for(String color:colors){
    15                 System.out.println(color);
    16             }
    17         }
    18     }
    View Code

    4、Map形式的JSON的GSON解析:

     1     /**
     2      * 解析一个map类型的json
     3      */
     4     public void testParseMapJson(){
     5         Gson gson = new Gson();
     6         Type type = new TypeToken<Map<String,UserBean>>(){}.getType();
     7         Map<String,UserBean> map = gson.fromJson(json3, type);
     8         Set<String> keys = map.keySet();
     9         for(String key:keys){
    10             UserBean bean = map.get(key);
    11             System.out.println(key);
    12             System.out.println(bean.getName());
    13         }
    14     }
    View Code

    5、将一个JavaBean对象封装成JSON格式

     1     /**
     2      * 将一个JavaBean对象封装成JSON格式
     3      */
     4     public String testJavaBean2Json(){
     5         UserBean userBean = new UserBean();
     6         userBean.setName("zhangsan");
     7         userBean.setAge(33);
     8         List<InterestBean> list = new ArrayList<InterestBean>();
     9         InterestBean bean1 = new UserBean().new InterestBean();
    10         bean1.setInterest("篮球1");
    11         InterestBean bean2 = new UserBean().new InterestBean();
    12         bean2.setInterest("篮球2");
    13         list.add(bean1);
    14         list.add(bean2);
    15         userBean.setInterests(list);
    16         //将User Bean转换成Json
    17         Gson gson = new Gson();
    18         String jsonStr = gson.toJson(userBean);
    19         System.out.println(jsonStr);
    20         return jsonStr;
    21     }
    22 
    23 }
    View Code

    仅供参考,如有雷同,纯属巧合^_^

  • 相关阅读:
    js5
    js4
    js(3)
    JS内容(2)
    html复习
    js介绍及内容(1)
    定位2
    position定位
    CSS
    列表及行块转变
  • 原文地址:https://www.cnblogs.com/lq147760524/p/7413835.html
Copyright © 2020-2023  润新知