• Document


    public static void main(String[] args) {
    Map map=new HashMap();
    map.put("我","妹");
    map.put("擦","哇");
    map.put("你","呀");
    JSONObject json = JSONObject.fromObject(map);
    System.out.println(json);
    }
    
    輸出的結果 {"我":"妹","擦":"哇","你":"呀"}
    
     
    
    toBean();
    
    首先一个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
  • 相关阅读:
    LeetCode-018-四数之和
    LeetCode-017-电话号码的字母组合
    LeetCode-016-最接近的三数之和
    LeetCode-015-三数之和
    LeetCode-014-最长公共前缀
    LeetCode-013-罗马数字转整数
    LeetCode-012-整数转罗马数字
    LeetCode-011-盛最多水的容器
    LeetCode-010-正则表达式匹配
    [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z形遍历
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/7374572.html
Copyright © 2020-2023  润新知