您的位置:首页 > Web前端 > JavaScript

本地解析Json

2015-08-03 15:23 706 查看
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: