json toBean使用

來源:互聯網
上載者:User

它有點類似於BeanUtils的copyProperties方法
具體的看代碼,一下就明白了!
首先一個javabean對象
public class Student {

    private int id ;
    private String name;
    private int age;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
   
    public String toString(){
        return this.id + ", " + this.name + ", " + this.age;
    }
}

然後測試toBean方法的類
import net.sf.json.JSONObject;

public class ToBeanTest {

    public static void main(String[] args) {
       
        String json = "{id:'1001',name:'張三',age:'22'}";

        Student stu = new Student();
        JSONObject obj = JSONObject.fromObject(json);
        stu = (Student)JSONObject.toBean(obj, Student.class);
        System.out.println(stu);
    }

}
輸出結果為1001, 張三, 22

然後我們在修改修改
import net.sf.json.JSONObject;

public class ToBeanTest {

    public static void main(String[] args) {
       
        String json = "{id:'1001',name:'張三'}";

        Student stu = new Student();
        JSONObject obj = JSONObject.fromObject(json);
        stu = (Student)JSONObject.toBean(obj, Student.class);
        System.out.println(stu);
    }

}
把年齡給去掉age為int型,輸出結果為:1001, 張三, 0

然後再做小小改動
import net.sf.json.JSONObject;

public class ToBeanTest {

    public static void main(String[] args) {
       
        String json = "{id:'1001',age:'22'}";

        Student stu = new Student();
        JSONObject obj = JSONObject.fromObject(json);
        stu = (Student)JSONObject.toBean(obj, Student.class);
        System.out.println(stu);
    }

}
把姓名給去掉name為String型,輸出結果為:1001, null, 22

再改動一下:
import net.sf.json.JSONObject;

public class ToBeanTest {

    public static void main(String[] args) {
       
        String json = "{id:'1001',name:'張三',age:'nn'}";

        Student stu = new Student();
        JSONObject obj = JSONObject.fromObject(json);
        stu = (Student)JSONObject.toBean(obj, Student.class);
        System.out.println(stu);
    }

}
把age改成非整形,輸出結果為:
1001, 張三, 0

再改動一下:
import net.sf.json.JSONObject;

public class ToBeanTest {

    public static void main(String[] args) {
       
        String json = "{id:'1001',name:'張三',age:'22',sex:'男'}";

        Student stu = new Student();
        JSONObject obj = JSONObject.fromObject(json);
        stu = (Student)JSONObject.toBean(obj, Student.class);
        System.out.println(stu);
    }

}
加了一個sex:'男'的一對索引值,輸出結果為:
1001, 張三, 22
2010-11-13 10:10:26 net.sf.json.JSONObject toBean
警告: Tried to assign property sex:java.lang.String to bean of class sky.liukai.Student

如上代碼就可瞭解toBean方法了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.