创建一个类,用来方法调用。如下,小伙伴用的时候可以直接调用。
public class SimulateNetAPI { /** * 获取去最原始的数据信息 * @return json data */ public static String getOriginalFundData(Context context) { InputStream input = null; try { //taipingyang.json文件名称 input = context.getAssets().open("taipingyang.json"); String json = convertStreamToString(input); return json; } catch (Exception e) { e.printStackTrace(); } return null; } /** * input 流转换为字符串 * * @param is * @return */ private static String convertStreamToString(InputStream is) { String s = null; try { //格式转换 Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A"); if (scanner.hasNext()) { s = scanner.next(); } is.close(); } catch (IOException e) { e.printStackTrace(); } return s; } }
调用如下
TaiBean mTai= JSONObject.parseObject(SimulateNetAPI.getOriginalFundData(mActivity),TaiBean.class); JsonMsg.getJsonMsg(mTai,"测试的数据");
JSONObject---阿里的fastjson,具体用法自己百度。
TaiBean 自己根据assets下面的.json文件创建的实体类
JsonMsg是封装的日志打印工具类。
举例如下。。。。
taipingyang.json文件
{ "code": "0000", "data": { "biRate": "0.32", "carInfoByPython": { "code": "0000", "data": { "biEndDate": "2018-10-05", "brandName": "长安SC7162A4轿车", "carkindCode": "客车", "ciEndDate": "2018-10-05", "companyCode": "picc", "damageBi": 0, "damageCi": 0, "engineNo": "84BE17915", "enrollDate": "2008-10-07", "fuelType": "A ", "insureInfo": [ { "idCard": "110102198601311534", "insuredCode": "1100100005542920", "mobile": "13910288510", "roleFlag": 7, "userName": "李艾森" } ], "isRenewal": "T", "itemKinds": [ { "amount": "29642.40", "insuranceCode": "damage", "insuranceName": "机动车损失保险", "premium": "600.62" }, { "amount": 0, "insuranceCode": "damage_exempt", "insuranceName": "不计免赔险(车损险)", "premium": "90.09" }, { "amount": "100000.00", "insuranceCode": "third", "insuranceName": "第三者责任保险", "premium": "335.45" }, { "amount": 0, "insuranceCode": "third_exempt", "insuranceName": "不计免赔险(三者险)", "premium": "50.32" } ], "licenseNo": "京NB1314", "modelCode": "LMAAND0073", "purchasePrice": "82800", "vehicleOwner": "李艾森", "vehicleVin": "LS5A33BEX8B058149" }, "msg": "success", "result": true }, "ciRate": "0.00", "glasstype": "1", "ins_company": "cpic", "ins_zone": "北京", "insurance": [ { "amount": "23681.0", "delFlag": "0", "exemptPrice": "0", "global": false, "insuranceCode": "damage", "page": false, "pageNo": 1, "pageSize": 10, "price": "500.52", "selected": "1", "version": 10 }, { "amount": "23681.0", "delFlag": "0", "exemptPrice": "15.65", "global": false, "insuranceCode": "robbery", "page": false, "pageNo": 1, "pageSize": 10, "price": "78.26", "selected": "2", "version": 10 } ], "reinsurance_renewal": "2", "seqNo": "1534991123104", "totalAmount": 47362, "totalPrice": 594.43, "trafficBeginDate": "2018-10-05", "vehicleModel": "长安SC7162A4轿车 2011款 舒适型", "vehiclePrice": 82800, "vehicleTypeId": "LMAAND0073" }, "result": true }
实体类
TaiBean
package com.insurance.car.mvp.model; import com.google.gson.annotations.SerializedName; import com.insurance.car.base.BaseModel; import java.util.List; public class TaiBean extends BaseModel { /** * data : {"biRate":"0.32","carInfoByPython":{"code":"0000","data":{"biEndDate":"2018-10-05","brandName":"长安SC7162A4轿车","carkindCode":"客车","ciEndDate":"2018-10-05","companyCode":"picc","damageBi":0,"damageCi":0,"engineNo":"84BE17915","enrollDate":"2008-10-07","fuelType":"A ","insureInfo":[{"idCard":"110102198601311534","insuredCode":"