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

最新文章

  1. 在requirejs中使用qunit
  2. 使用kuernetes提供高可用的kibana服务
  3. iOS 地图
  4. MyEclipse 启动tomcat时报错:Cannot change deployment state from ERROR to REDEPLOYING.ds
  5. [一]初识Ajax
  6. BZOJ 1021 [SHOI2008]Debt 循环的债务
  7. Android动画效果
  8. c#线程的几种启动方法
  9. WinForm响应式布局设计实践
  10. iOS简单动画效果:闪烁、移动、旋转、路径、组合
  11. Codeforces 623D [Amazing概率题]
  12. B. Array
  13. python爬虫-基础入门-python爬虫突破封锁
  14. js问题 项目问题
  15. [UE4]蓝图调试
  16. MongoDB3.2配置文件.md
  17. Cosplay之孩子的妈咪
  18. find_circ 识别circRNA 的原理
  19. 安装glibc-2.14
  20. 第七篇Python基本数据类型之数字&字符串&布尔值

热门文章

  1. Ajax基础原理与应用
  2. 在CSS中,link里 的rel="stylesheet"是什么意思?
  3. (转)Android访问webservice
  4. 【巨杉数据库Sequoiadb】点燃深秋,巨杉数据库亮相DTC数据技术嘉年华大会
  5. luogu P2158 [SDOI2008]仪仗队 (欧拉函数)
  6. MongoDB的安装问题
  7. (转)spring 框架介绍
  8. 全栈高级web前端工程师的必经之路
  9. 线程锁Lock ,Rlock
  10. SpringBoot学习- 8、整合Shiro