用这个api有时候会报错,基本上是因为包没引入完,
引入这几个包后基本上没什么问题,
但是对于JSONObject.fromObject 和SONObject.toBean的使用为:
String strTmp = "{\"errmsg\":\"ok\",\"ErrorCode\":0,\"messageId\":\"f5d4566c4c833152b3feb42a8092ee6c\",\"invaliduser\":\"\",\"forbiddenUserId\":\"\",\"resJson\":{\"errmsg\":\"ok\",\"forbiddenUserId\":\"\"}}"; JSONObject jb = JSONObject.fromObject(strTmp); Map<String,Object> resmap = (Map<String,Object>)jb; System.out.println("-----resmap:---"+resmap.toString()); 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);
package bao; 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; } }
结果为:
-----resmap:---{"errmsg":"ok","ErrorCode":0,"messageId":"f5d4566c4c833152b3feb42a8092ee6c","invaliduser":"","forbiddenUserId":"","resJson":{"errmsg":"ok","forbiddenUserId":""}} 1001,==== 张三,22