本地解析Json
1、把j资源放在res/raw下
2、把需要解析的所有字符取出来,对象为jsonObect
//将json文件读取到buffer数组中 InputStream is = this.getResources().openRawResource(R.raw.getcart); byte[] buffer = new byte[is.available()]; is.read(buffer); // 将字符数组转换为UTF-8编码的字符串 String json = new String(buffer, "UTF-8"); JSONObject jsonObject = new JSONObject(json);
3、如果是{}:用JSONObject,如果是[]:则用JSONArray取出来(重点,理解就很简单了)
JSONObject objectInfo = jsonObject.getJSONObject("info");
JSONArray arraygroup = objectInfo.getJSONArray("group");
4、取到根时,则:
int stock_id = jtem.getInt("stock_id"); String goods_name = jtem.getString("goods_name"); int qty = jtem.getInt("qty"); boolean is_choose = jtem.getBoolean("is_choose"); int price = jtem.getInt("price"); String goods_img = jtem.getString("goods_img"); String extend = jtem.getString("extend"); int stock_num = jtem.getInt("stock_num"); int goodsTotalPrice = jtem.getInt("goodsTotalPrice"); double rate = jtem.getDouble("rate");
最后附上源码给大家看看
http://download.csdn.net/detail/azhansy/8956277
版权声明:本文为博主原创文章,未经博主允许不得转载。