java处理json数据

本文详细介绍了JSON中的数组(JsonArray)和对象(JsonObject)的概念,并通过Java代码示例展示了如何进行基本操作,包括创建、添加元素以及从Java数据结构转换为JSON文本。同时,还演示了如何读取JSON文件并访问其内容,强调了JSONArray和JSONObject的访问方式。
摘要由CSDN通过智能技术生成

一.json中的数组和对象
    1.数组(JsonArray)
        数组用一对[],表示存放的是一般的数组数据。
        如:["11","22","33"],表示这是一个JSONArray数组,里面有3个数据:”11“,”22“,”33“。当然可以是复杂的数据,就是所谓的嵌套定义吧。
    2.对象(JsonObject)
        对象用一对{},来表示其中JSON通用的”键-值“对。
        如:{"sex1":"female","name0":"zhangsan"},表示的是一个JSON对象,里面有两组数据(键值对),sex1=female,name0=zhangsan。当然这里键对应的数据值,可以是复杂的JSON对象或者数组。
二.基本的JSONArray和JSONObject操作
  

 import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    public class ObjectAndArray1 {
        public static void main(String args[])
        {
            JSONObject jsonObj  = new JSONObject();
            jsonObj.put("name0", "zhangsan");
            jsonObj.put("sex1", "female");
            System.out.println(jsonObj);    //输出为:{"sex1":"female","name0":"zhangsan"}
            
            JSONArray jsonArray = new JSONArray();
            jsonArray.add("11");
            jsonArray.add("22");
            jsonArray.add("33");
            System.out.println(jsonArray);    //输出为:["11","22","33"]
        }
    }


三.由java自带的数据结构转换为JSON文本
  

 import java.util.HashMap;
    import java.util.Map;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    public class ObjectAndArray2{
        public static void main(String args[])
        {
            //可以由数组,列表等生成JSONArray
            String list[]={"11","22"};
            JSONArray jsonarray = JSONArray.fromObject(list); 
            jsonarray.add("33");
            System.out.println(jsonarray);    //输出为:["11","22","33"]
            
            //可以由Map生成JSONObject
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("NO1", "第一个");
            map.put("NO2", "第二个");
            map.put("NO3", jsonarray);
            JSONObject jsonObj  = JSONObject.fromObject(map);
            System.out.println(jsonObj);    //输出为:{"NO3":["11","22","33"],"NO2":"第二个","NO1":"第一个"}
        }
    }


四.读取JSON文件
    JSONArray必须用下标读取内部数据。
    JSONObject必须用"键"读取对应的"值"。
    

import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    public class ObjectAndArray {
        public static void main(String args[])
        {
            JSONArray jsonarray;
            JSONObject jsonObj;
            //读取JSONArray,用下标索引获取
            String array="[\"11\",\"22\",\"33\"]";
            jsonarray = JSONArray.fromObject(array); 
            System.out.println(jsonarray.getString(1));    //输出为:22
            
            //读取JSONObject
            String object="{\"NO1\":[\"44\",\"55\",\"66\"],\"NO2\":{\"NO1\":\"第一个\"}}";
            jsonObj  = JSONObject.fromObject(object);
            System.out.println(jsonObj.get("NO1"));    //输出为:["44","55","66"]
            
            jsonarray = (JSONArray)(jsonObj.get("NO1")); 
            System.out.println(jsonarray.getString(1));    //输出为:55
            
            //用"键"获取值
            jsonObj=(JSONObject)jsonObj.get("NO2");
            System.out.println(jsonObj);    //输出为:{"NO1":"第一个"}
            
        }
    }


    注:get("")方法用于获取JSONObject的数据    getString(Integer) 用于JSONArray的数据
五.JSONArray和JSONObject的常用方法

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
处理嵌套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<o.length(); i++){ name = JSONObject.getNames(o)[i]; System.out.println("商品项ID:"+name); t_o = o.getJSONObject(name); for(int j=0; j< t_o.length(); j++){ name = JSONObject.getNames(t_o)[j]; System.out.print(name+":"+t_o.get(name)+" "); } System.out.println(); } }catch(Exception e){ e.printStackTrace(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值