引入的包 :json-lib-2.1.jar
import net.sf.json.JSONArray; import net.sf.json.JSONObject;
//随意创建一个实体 RuleDomain rule=new RuleDomain(); rule.setCityId("3"); rule.setProvinceId("2"); //对象转json字符串 JSONObject jsonObj=JSONObject.fromObject(rule); System.out.println(jsonObj.toString()); //json字符串转对象 JSONObject json=JSONObject.fromObject(jsonObj.toString()); RuleDomain ruleDomain=(RuleDomain) JSONObject.toBean(json, RuleDomain.class); //此次是转换之后的类 System.out.println(ruleDomain.getCityId());