json案例

List<Province>list=newArrayList<Province>();

Provincep1=newProvince(1,"吉林省");

Provincep2=newProvince(2,"辽宁省");

Provincep3=newProvince(3,"山东省");

list.add(p1);

list.add(p2);

list.add(p3);

/****************************************************************************************************/

/*JSONArrayjsonArray=JSONArray.fromObject(list);

//[{"pname":"吉林省","pid":1},{"pname":"辽宁省","pid":2},{"pname":"山东省","pid":3}]

System.out.println(jsonArray.toString());*/

/****************************************************************************************************/

JsonConfigconfig=newJsonConfig();

//过滤list集合中存放的Province这个对象的pid的属性不生成json

config.setExcludes(newString[]{"pid"});

JSONArrayjsonArray=JSONArray.fromObject(list,config);

//[{"pname":"吉林省"},{"pname":"辽宁省"},{"pname":"山东省"}]//这样就把pid去掉啦

System.out.println(jsonArray.toString());

/****************************************************************************************************/

//一个对象转换成json格式

Provincep=newProvince(1,"吉林省");

JSONObjectjsonObject=JSONObject.fromObject(p);

//{"pid":1,"pname":"吉林省"}

System.out.println(jsonObject);

相关推荐