首页 > 代码库 > Json4:使用json-lib解析、生成Json

Json4:使用json-lib解析、生成Json

特征:
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}
木母

Json4:使用json-lib解析、生成Json