JSONObject.fromObject(map) 执行报错.

前端之家收集整理的这篇文章主要介绍了JSONObject.fromObject(map) 执行报错.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



用这个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

原文链接:https://www.f2er.com/json/288856.html

猜你在找的Json相关文章