zoukankan      html  css  js  c++  java
  • fastjson解析任意json

    fastjson解析任意json到bean

    解析案例的代码
    package com.base.config;
    
    import java.util.List;
    
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    public class JsonParse {
        
        public static void main(String[] args) {
            
            String arrJson = "[{"id":1,"mobile":15809619172},{"id":2,"mobile":14588827746}]";
            List<Student> stus = JSONArray.parseArray(arrJson, Student.class);
            
            System.out.println("array json的解析结果:");
            
            for (Student stu : stus) {
                System.out.println(stu);
            }
            
            String beanJson = "{"id":1,"mobile":15809619172}";
            Student stu = JSONObject.parseObject(beanJson, Student.class);
            System.out.println("beanjson的解析结果:");
            System.out.println(stu);
            
        }
    
    }
    Student的代码
    package com.base.config;
    
    public class Student {
        private int id;
        private String mobile;
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getMobile() {
            return mobile;
        }
        public void setMobile(String mobile) {
            this.mobile = mobile;
        }
        
        @Override
        public String toString() {
            return "Student [id=" + id + ", mobile=" + mobile + "]";
        }
    
    }
    程序运行结果
    array json的解析结果:
    Student [id=1, mobile=15809619172]
    Student [id=2, mobile=14588827746]
    beanjson的解析结果:
    Student [id=1, mobile=15809619172]

    这样解析效率很高,而且代码非常简单。比json-lib库要简化非常多。所以强烈推荐使用fastjson.

  • 相关阅读:
    .net mvc 路由
    Dos小技巧-在Dos中直接打开软件
    Dos操作基础
    使用uiautomator时遇到问题的处理方法
    3.UiObejct API 详细介绍
    2.UiSelector API 详细介绍
    腾讯加固纯手工简易脱壳教程
    手脱nSPack 3.7
    Servlet各版本web.xml的头文件配置模板
    dynamic web module 版本之间的区别
  • 原文地址:https://www.cnblogs.com/sfmjp/p/5742139.html
Copyright © 2011-2022 走看看