java后台封装和解析json数据

//1.封装集合
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray1 = JSONArray.fromObject( list );
System.out.println("集合:"+jsonArray1);
//2.封装数组
int[] Array ={ 1, 2, 3 };
JSONArray jsonArray2 = JSONArray.fromObject(Array);
System.out.println("数组:"+jsonArray2);
//3.封装一般数据
   JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
   System.out.println("一般数据:"+jsonArray3);
   //3.封装map
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject jsonObject1 = JSONObject.fromObject(map);
System.out.println("map:"+jsonObject1);
//3.封装对象
Student student=new Student();
student.setAge(20);
student.setName("张三");

JSONObject jsonObject2 =JSONObject.fromObject(student);
String jsonString=jsonObject2.toString();
//解析json
JSONObject jsonObject3 =JSONObject.fromObject(jsonString);

System.out.println("姓名:"+jsonObject3.get("name")+"\t"+"年龄:"+jsonObject3.get("age"));


JAR包简介


      1.commons-lang.jar


      2.commons-beanutils.jar


      3.commons-collections.jar


      4.commons-logging.jar 


      5.ezmorph.jar


      6.json-lib-2.2.2-jdk15.jar


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值