• Java对【JSON数据的解析】--fastjson解析法


    要求:解析下面JSON数据

    String string = “{no:1,name:’Android’,employees:[{name:’zhangsan’,age:20},{name:’lisi’,age:21},{name:’wangwu’,age:22}]}”;


    代码:

    package com.qf.demo4;
    
    import java.util.ArrayList;
    
    import com.alibaba.fastjson.JSON;
    
    public class Test {
    
        public static void main(String[] args) {
            //fastjson解析数据
            //1.阿里解析过程和Gson类似,
            String string= "{no:1,name:'android',employees:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";
    
            Employ employ = JSON.parseObject(string,Employ.class);
            System.out.println(employ);
        }
    }
    
    class Person{
        private String name;
        private int age;
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public Person() {
            super();
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    
    
    }
    
    
    class Employ{
        private int no;
        private String name;
        private ArrayList<Person> employees;
        public Employ(int no, String name, ArrayList<Person> employees) {
            super();
            this.no = no;
            this.name = name;
            this.employees = employees;
        }
        public Employ() {
            super();
        }
        public int getNo() {
            return no;
        }
        public void setNo(int no) {
            this.no = no;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public ArrayList<Person> getEmployees() {
            return employees;
        }
        public void setEmployees(ArrayList<Person> employees) {
            this.employees = employees;
        }
        @Override
        public String toString() {
            return "Employ [no=" + no + ", name=" + name + ", employees=" + employees + "]";
        }
    
    }
  • 相关阅读:
    uva-11129-分治
    HDU 6016
    POJ 2142
    CodeForces 778B
    CodeFroces 758C
    HDU 5900
    HDU 5903
    HDU 5904
    POJ 3080
    HDU 5195
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6854002.html
Copyright © 2020-2023  润新知