android读取assets下的.json文件,并且转化为实体类

这篇博客介绍了如何在Android应用中读取assets目录下的.json文件,并使用Fastjson将其内容转换为自定义的实体类TaiBean。通过提供的代码示例,开发者可以学习到具体的调用方法和实现步骤。
摘要由CSDN通过智能技术生成

创建一个类,用来方法调用。如下,小伙伴用的时候可以直接调用。

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":"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值