Android的本地Json解析
1、本地
取出需要解析的Json字符串
转化为UTF-8的格式
将字符串json转换为json对象
根据Json里面的key读取数据
//将json文件(存放在本地的res/raw)读取到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"); 将字符串json转换为json对象,以便于取出数据 JSONObject jsonObject = new JSONObject(json); //解析info数组,解析中括号括起来的内容就表示一个数组,使用JSONArray对象解析 JSONObject objectInfo = jsonObject.getJSONObject("info"); JSONArray arraygroup = objectInfo.getJSONArray("group"); for (int i = 0; i < arraygroup.length(); i++) { ShopBean shopBean = new ShopBean(); JSONObject item = arraygroup.getJSONObject(i); int seller_uid = item.getInt("seller_uid"); String seller_name = item.getString("seller_name"); shopBean.setSeller_uid(seller_uid); shopBean.setSeller_name(seller_name); JSONArray arraygoods = item.getJSONArray("goods"); for (int j = 0; j < arraygoods.length(); j++) { JSONObject jtem = arraygoods.getJSONObject(j); 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"); //把商品的属性值添加到shop实体类中 shopBean.setStock_id(stock_id); shopBean.setGoods_name(goods_name); shopBean.setQty(qty); shopBean.setIs_choose(is_choose); shopBean.setPrice(price); shopBean.setGoods_img(goods_img); shopBean.setExtend(extend); shopBean.setStock_num(stock_num); shopBean.setGoodsTotalPrice(goodsTotalPrice); shopBean.setRate(rate); shopBeanList.add(shopBean); }
另附:
定义的实体类为:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ShopBean { private int seller_uid;//店铺ID private String seller_name;//店铺名称 private int stock_id;//商品ID private String goods_name;//商品名称 private int qty;//商品的星级 private boolean is_choose;//是否选中 private int price;//单价 private String goods_img;//图片链接 private String extend;//图片拓展 private int stock_num;//商品数量 private int goodsTotalPrice;//商品总价 private double rate;//商品税率 public int getSeller_uid() { return seller_uid; } public void setSeller_uid(int seller_uid) { this.seller_uid = seller_uid; } public String getSeller_name() { return seller_name; } public void setSeller_name(String seller_name) { this.seller_name = seller_name; } public int getStock_id() { return stock_id; } public void setStock_id(int stock_id) { this.stock_id = stock_id; } public String getGoods_name() { return goods_name; } public void setGoods_name(String goods_name) { this.goods_name = goods_name; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public boolean is_choose() { return is_choose; } public void setIs_choose(boolean is_choose) { this.is_choose = is_choose; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getGoods_img() { return goods_img; } public void setGoods_img(String goods_img) { this.goods_img = goods_img; } public String getExtend() { return extend; } public void setExtend(String extend) { this.extend = extend; } public int getStock_num() { return stock_num; } public void setStock_num(int stock_num) { this.stock_num = stock_num; } public int getGoodsTotalPrice() { return goodsTotalPrice; } public void setGoodsTotalPrice(int goodsTotalPrice) { this.goodsTotalPrice = goodsTotalPrice; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } @Override public String toString() { return "商品ID: " + stock_id + "\n" + "商品名称: " + goods_name + "\n\n" + "商品等级: " + qty + "\n" + "是否选中: " + is_choose + "\n" + "商品单价: " + price + "\n" + "商品拓展: " + extend + "\n" + "商品数量: " + stock_num + "\n" + "商品总价: " + goodsTotalPrice + "\n" + "商品税率: " + rate + "\n" ; } }
本地的Json:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{
"info": {
"group": [
{
"seller_uid": "375",
"seller_name": "wendy\u7f8e\u5986\u4e13\u8425\u5e97",
"goods": [
{
"stock_id": 6019447,
"goods_name": "\u4e13\u67dc\u6b63\u54c1 \u9999\u5948\u513f \u9ec4\u8272\u9082\u9005\u9999\u6c34\u6da6\u4f53\u4e73\u8eab\u4f53\u971c200ml",
"qty": 1,
"is_choose": true,
"price": 404,
"goods_img": "http:\/\/sjshop.easyder.com\/res\/upload\/image\/100-100\/2d\/0f\/2d0fc9d2b4af836313243dc53e2e07e1.jpg",
"extend": "",
"stock_num": "3",
"goodsTotalPrice": 404,
"rate": 3.23
}
]
},
{
"seller_uid": "285",
"seller_name": "\u4e13\u8425\u5e97",
"goods": [
{
"stock_id": 8773389,
"goods_name": "[\u7279\u4ef7\u5305\u90ae]\u4e13\u67dc\u6b63\u54c1 \u897f\u57df\u7f8e\u519c \u590f\u679c\u6fb3\u6d32\u8fdb\u53e3\u96f6\u98df\u590f\u5a01\u5937\u679c\u575a\u679c\u98df\u54c1\u5976\u6cb9\u9999\u5473\u5e72\u679c\u96f6\u98df160g",
"qty": 2,
"is_choose": true,
"price": 6.19,
"goods_img": "http:\/\/sjshop.easyder.com\/res\/upload\/image\/100-100\/20\/b8\/20b89870c6babf6fb36565b7e501d58f.jpg",
"extend": "",
"stock_num": "4",
"goodsTotalPrice": 12.38,
"rate": 0.04
},
{
"stock_id": 8655629,
"goods_name": "\u4e13\u67dc\u6b63\u54c1 \u4e45\u7231 love\u60c5\u4fa3\u6212\u6307 925\u7eaf\u94f6\u5bf9\u6212\u5b50 \u745e\u58eb\u94bb\u9996\u9970\u54c1 \u514d\u8d39\u523b\u5b57",
"qty": 2,
"is_choose": true,
"price": 99,
"goods_img": "http:\/\/sjshop.easyder.com\/res\/upload\/image\/100-100\/0a\/91\/0a910ae6a7ca2f2081fe4e87928ae9df.jpg",
"extend": "\u7537\u6b3e21\u53f7 ",
"stock_num": "5",
"goodsTotalPrice": 198,
"rate": 3.17
}
]
},
{
"seller_uid": "1147",
"seller_name": "\u4e0a\u6d77\u60a0\u8dc3\u5bb6\u5c45\u7528\u54c1\u6709\u9650\u516c\u53f8",
"goods": [
{
"stock_id": 8738701,
"goods_name": "\u4e13\u67dc\u6b63\u54c1 \u8c61\u5370 \u5973\u58eb\u7537\u58eb\u513f\u7ae5\u4fdd\u6e29\u676f\u5b50\u65c5\u884c\u5fc5\u5907\u4fdd\u6e29\u6c34\u58f6 SF-CC18-XA 1800m",
"qty": 3,
"is_choose": true,
"price": 539,
"goods_img": "http:\/\/sjshop.easyder.com\/res\/upload\/image\/100-100\/65\/54\/65541e52cbfb7d187ac219bc96b2db68.jpg",
"extend": "",
"stock_num": "27",
"goodsTotalPrice": 1617,
"rate": 12.94
}
]
},
{
"seller_uid": "1093",
"seller_name": "\u59da\u5e02\u7530\u91ce\u5851\u6599\u5236\u54c1\u6709\u9650\u516c\u53f8",
"goods": [
{
"stock_id": 8696621,
"goods_name": "\u4e13\u67dc\u6b63\u54c1 \u5929\u548c\u7530\u91ce \u52a0\u539a\u94dd\u5408\u91d1\u843d\u5730\u6298\u53e0\u667e\u8863\u67b6 X\u578b\u7f6e\u5730\u9633\u53f0\u8863\u67b6 \u667e\u88ab\u67b6\u5305\u90ae",
"qty": 2,
"is_choose": true,
"price": 188.65,
"goods_img": "http:\/\/sjshop.easyder.com\/res\/upload\/image\/100-100\/d8\/ae\/d8ae683db4cdedf750ea7b4e713bfd1e.jpg",
"extend": "\u91d1\u8272 ",
"stock_num": "274",
"goodsTotalPrice": 377.3,
"rate": 3.02
}
]
}
]},
"tips": "\u6210\u529f",
"status": 1,
"chooseGoodsNum": 10,
"chooseGoodsTotalPrice": 2608.68,
"allGoodsTotalPrice": 2608.68,
"allGoodsNum": 10,
"totalRate": 22.4
}