标签:import code [] img tostring parse .com str 技术
特征:
1.包多
2.JSONObject.fromObject
import net.sf.json.JSONObject; public class JsonLib { public static void main(String[] args) { Bean2Json(); Json2Bean(); } static private void Bean2Json() { People p = new People(); p.setName("金公"); p.setPower(10000); // 关键代码 JSONObject jspnObj = JSONObject.fromObject(p); System.out.println(jspnObj.toString()); } static private void Json2Bean() { String strJson = "{‘name‘:‘木母‘,‘power‘:8000}"; JSONObject jspnObj = JSONObject.fromObject(strJson); // 关键代码 People p = (People) JSONObject.toBean(jspnObj, People.class); System.out.println(p.getName()); } static private void parseJson() { } }
运行结果:
{"name":"金公","power":10000}
木母
标签:import code [] img tostring parse .com str 技术
原文地址:http://www.cnblogs.com/AndyHoo/p/6376002.html