日期:2014-05-16  浏览次数:20414 次

java 处理 json数据

?

处理嵌套json格式的数据。。。

?

public static void main(String[] args) {

// 官方API ?http://www.json.org/java/

/*

购物车中信息

goods_cart=%7Bcart_1325036696007:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg%22,specs:%22b555bfj05d7dcg307h91323398584156%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E9%BB%91%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AL%22,price:4765,stock:15,count:6%7D,cart_1325036702105:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg%22,specs:%22787a9f5he93chcifh951323398314484%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E9%BB%91%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AXL%22,price:4700.15,stock:12,count:1%7D,cart_1325136643984:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg%22,specs:%228466347bi6eia43hd6j1323398639859%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E7%81%B0%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AXL%22,price:4600,stock:3,count:1%7D%7D;?

?

* **/

try{

String s0 = "%7Bcart_1325036696007:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg%22,specs:%22b555bfj05d7dcg307h91323398584156%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E9%BB%91%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AL%22,price:4765,stock:15,count:6%7D,cart_1325036702105:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg%22,specs:%22787a9f5he93chcifh951323398314484%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E9%BB%91%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AXL%22,price:4700.15,stock:12,count:1%7D,cart_1325136643984:%7Bgoods_id:%22100015%22,goods_name:%22%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A%E8%BF%9B%E5%8F%A3%E7%BB%B5%E7%BE%8A%22,goods_imgsrc:%22http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg%22,specs:%228466347bi6eia43hd6j1323398639859%22,specsstr:%22%E9%A2%9C%E8%89%B2%EF%BC%9A%E7%81%B0%E8%89%B2%E3%80%80%E5%B0%BA%E5%AF%B8%EF%BC%9AXL%22,price:4600,stock:3,count:1%7D%7D;";

String s= java.net.URLDecoder.decode(s0, "utf-8");

System.out.println(s);

JSONObject o = new JSONObject(s);

System.out.println(o.get("cart_1325036696007")); //根据属性,获取值

System.out.println(o.toString()); //得到字符串

System.out.println(o.names().get(2)); //获取对象中第三组属性名

System.out.println(o.names().length()); //获取对象中属性个数

//System.out.println(o.names().