Tears_fg

导航

JAVA_FastJson

package com.qf.mobiletrain01;

import java.util.List;

import com.alibaba.fastjson.JSON;

class Student {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student() {
        super();
    } 

}

public class TestFastJson {
    public static void main(String[] args) {
        String json = "{name:'张三',age:18}";
        //注意:需要导入fastjson-1.1.22.jar包 
        // 1)json数据必须正规 2)java实体类需要有无参构造方法 和set方法 3)实体类不能是内部类 4).自定义的属性与对象名一致才能被解析
        // json-->javaBean parseObjectFastJson解析json数据 

        System.out.println(JSON.parseObject(json, Student.class));

        String json2 = "[{name:'张三',age:18},{name:'李四',age:29}]";
        // json-->集合 parseArray();
        List<Student> list = JSON.parseArray(json2, Student.class);
        System.out.println(list);

    }

}

 

posted on 2016-07-31 15:26  Tears_fg  阅读(221)  评论(0编辑  收藏  举报