关于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 decode = Codec.decode(encode, "UTF-8");
测试字符加密编码的正确性,在上一篇中有代码。