android程序中json解析小例子

关于json我就不说了。

这里只是本地的数据,没有模拟客户端和服务器交互。


JSONArray jsonArray;
        JSONObject jsonObject;
        String json = "[{room_id: '1001', "
                      + "ms_ip: '192.168.1.227', "
                      + "ms_port: '80',"
                      + "txt_ip: '192.168.1.228',"
                      + "txt_port: '80',"
                      + "room_name: '激情视频', "
                      + "room_pic: 'http://room.imichat.com/room/2011/10/09/jqsp.png', "
                      + "order_status: '1' ,"
                      + "try_status : '1', "
                      + "try_time: '5', "
                      + "induction: '看了后悔30年,不看后悔80年', "
                      + "room_pic_num : '2', "
                      + "room_pic_num_url: [{room_pic:'http://room.imichat.com/room/2011/10/09/jqsp.png'},{room_pic:'http://room.imichat.com/room/2011/10/09/jqsp.png'}],"
                      + "room_order_way: [{bill_name:'aaa', price:'10', content:'1小时/元'}]"
                      +"}]";
        String encode = Codec.encode(json, "UTF-8");
        String decode = Codec.decode(encode, "UTF-8");
        try {
            jsonArray = new JSONArray(decode);
            System.out.println(jsonArray.toString());
            for(int i = 0; i < jsonArray.length(); i++){
                jsonObject = (JSONObject) jsonArray.getJSONObject(i);
                System.out.println(jsonObject.getInt("room_id")
                                   + "\n" + jsonObject.getString("ms_ip")
                                   + "\n" + jsonObject.getString("ms_port")
                                   + "\n" + jsonObject.getString("txt_ip")
                                   + "\n" + jsonObject.getString("txt_port")
                                   + "\n" + jsonObject.getString("room_name")
                                   + "\n" + jsonObject.getString("room_pic")
                                   + "\n" + jsonObject.getString("order_status")
                                   + "\n" + jsonObject.getString("try_status")
                                   + "\n" + jsonObject.getString("try_time")
                                   + "\n" + jsonObject.getString("induction")
                                   + "\n" + jsonObject.getString("room_pic_num")
                                   );
                JSONArray roomPicNumUrlArray = new JSONArray(jsonObject.getString("room_pic_num_url"));
                for(int j =0; j < roomPicNumUrlArray.length(); j++){
                    JSONObject roomPicNumUrlObject = roomPicNumUrlArray.getJSONObject(j);
                    System.out.println("\n\t"+roomPicNumUrlObject.getString("room_pic")+"\n");
                }
                JSONArray roomOrderWayArray = new JSONArray(jsonObject.getString("room_order_way"));
                System.out.println(roomOrderWayArray.toString());
                for(int k =0; k < roomOrderWayArray.length(); k++){
                    JSONObject roomOrderWayObject = roomOrderWayArray.getJSONObject(k);
                    System.out.println("\t"+roomOrderWayObject.getString("bill_name")
                                       +"\t"+roomOrderWayObject.getString("price")
                                       +"\t"+roomOrderWayObject.getString("content")+"\n");
                }
                
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        String encode = Codec.encode(json, "UTF-8");

        String decode = Codec.decode(encode, "UTF-8");

测试字符加密编码的正确性,在上一篇中有代码。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值