• JSON解析


    1.JSON概念

    (Java   Script   Object  Notation)是一种轻量级的数据交换格式,其本质是一种特定的字符串格式。

    2.规则

    集合[]和对象{}两种

    显示方式为:[value1,value2],{key1:value1,key2:value2}

    [{key1:value1},{key2:value2}...]{key1(字符串):[value1]}

    3.解析

    JSON解析有解析工具,可以分为好多,这里就说两种官方JSON解析jar包和阿里巴巴的JSON解析jar包

    JSON解析形式上分为两种:

    【1】.从对象到(集合)JSON字符串

    【2】.从JSON字符串到对象(集合)

    具体操作代码如下,导入jar包的方法就不再写了

    1.先有一个实体类为后面测试JSON解析做准备

    package com.hanqi.test;
    
    import java.util.Date;
    
    public class User {
    
        private int userID;
        private String userName;
        private String password;
        private Date birthday;
        
        public int getUserID() {
            return userID;
        }
        public void setUserID(int userID) {
            this.userID = userID;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        public User(int userID, String userName, String password) {
            super();
            this.userID = userID;
            this.userName = userName;
            this.password = password;
        }
        public User() {
            super();
            // TODO Auto-generated constructor stub
        }
        @Override
        public String toString() {
            return "User [userID=" + userID + ", userName=" + userName + ", password=" + password + ", birthday=" + birthday
                    + "]";
        }
    
    }

    2.测试代码如下:

    package com.hanqi.test;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    
    import com.alibaba.fastjson.JSONObject;
    
    public class TestJson {
    
        
        public static void main(String[] args)
        {
            
            //测试JSON解析
            
            // 1 从对象到(集合)JSON字符串
            
            User u1 = new User(999,"admin","1234");
            
            u1.setBirthday(new Date());
            
            String ju1=JSONObject.toJSONString(u1);
            
            System.out.println("ju1="+ju1);
            
            // 集合
            List<User> lu = new ArrayList<User>();
            lu.add(new User(111,"User1","111"));
            lu.add(new User(222,"User2","111"));
            lu.add(new User(333,"User3","111"));
            lu.add(new User(444,"User4","111"));
            lu.add(new User(555,"User5","111"));
            
            String jlu=com.alibaba.fastjson.JSONArray.toJSONString(lu);
            
            System.out.println("jlu="+jlu);
            
            
            
            
            // 2 从JSON字符串到对象(集合)
            
            User u2=(User)JSONObject.parseObject(ju1, User.class);
            
            System.out.println(u2); 
            
            try {
                    org.json.JSONObject jo = new  org.json.JSONObject(ju1);
                    
                    int userid = jo.getInt("userID");
                    
                    System.out.println("userID="+userid);
                } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            // 字符串到集合
            List<User> lu2 = com.alibaba.fastjson.JSONArray.parseArray(jlu, User.class);
            
            for(User u:lu2)
            {
                System.out.println(u);
            }
            
            try {
                    JSONArray ja = new JSONArray(jlu);
                    
                    org.json.JSONObject u3= ja.getJSONObject(0);
                    
                    System.out.println("u3="+u3);
                } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    实用的.net小工具整理
    HTTP Analyzer——WEB调试代理
    JavaScript组件之JQuery(A~Z)教程(基于Asp.net运行环境)[示例代码下载]
    JAVA操作Excel表格
    java笔记一:类成员的初始化顺序
    javaMail发邮件
    初学hibernate框架
    Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)转帖
    JAVA WEB开发中处理乱码汇总
    抽象工厂之更换皮肤
  • 原文地址:https://www.cnblogs.com/miracle-0807/p/6102843.html
Copyright © 2020-2023  润新知