文件名称:java 处理 json数据
文件大小:21KB
文件格式:RAR
更新时间:2015-01-25 06:25:44
json JSONObject
处理嵌套json格式的数据。。。
public static void main(String[] args) {
// 官方API http://www.json.org/java/
/*
购物车中信息
goods_cart={cart_1325036696007:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"b555bfj05d7dcg307h91323398584156",specsstr:"颜色:黑色 尺寸:L",price:4765,stock:15,count:6},cart_1325036702105:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"787a9f5he93chcifh951323398314484",specsstr:"颜色:黑色 尺寸:XL",price:4700.15,stock:12,count:1},cart_1325136643984:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg",specs:"8466347bi6eia43hd6j1323398639859",specsstr:"颜色:灰色 尺寸:XL",price:4600,stock:3,count:1}};
* **/
try{
String s0 = "{cart_1325036696007:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"b555bfj05d7dcg307h91323398584156",specsstr:"颜色:黑色 尺寸:L",price:4765,stock:15,count:6},cart_1325036702105:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100016_00948703_68.jpg",specs:"787a9f5he93chcifh951323398314484",specsstr:"颜色:黑色 尺寸:XL",price:4700.15,stock:12,count:1},cart_1325136643984:{goods_id:"100015",goods_name:"澳大利亚进口绵羊",goods_imgsrc:"http://192.168.1.180:7001//gwadmin/uploadimg/spxc/2011/12/9/100015_00399656_68.jpg",specs:"8466347bi6eia43hd6j1323398639859",specsstr:"颜色:灰色 尺寸:XL",price:4600,stock:3,count:1}};";
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().getJSONArray(1)); //获取对象中属性个数
//names(jsonObjectName) 私有方法 获取该对象的所有属性名,返回成JSONArray。
//JSONObject.getNames(jsonObjectName) 静态方法 获取对象的所有属性名,返回成数组。
System.out.println(JSONObject.getNames(o.getJSONObject("cart_1325036696007"))[1]);
System.out.println(o.getJSONObject("cart_1325036696007").names().get(1));
System.out.println(o.length()); //共有几组对象
System.out.println(o.has("cart_1325036696007")); //有无该该值
/* 遍历json中的每一组元素*/
String name = null;
JSONObject t_o = null;
for(int i=0; i
【文件预览】:
json
----JSONObject.java(54KB)
----Test.java(4KB)
----JSONException.java(700B)
----JSONArray.java(29KB)
----JSONTokener.java(13KB)
----JSONString.java(712B)