• android 解析json文件


    怎样解析data.json文件并且保存为Config对象?

    第一步:使用数据流转换成字符串

    private String loadConfig() {
    InputStream is = null;
    ByteArrayOutputStream bos = null;
    try {
    is = getAssets().open("data.json");
    bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    int len;
    while ((len = is.read(b)) != -1) {
    bos.write(b, 0, len);
    }
    return bos.toString("utf-8");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (bos != null)
    bos.close();
    if (is != null)
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return null;
    }
    第二步:使用fastjson包
    String json = loadConfig();
    if (json != null) {
    List<Config> configs = JSON.parseArray(json, Config.class);
    }

    这里的configs集合就是json文件里的数据,注意json里的key是和实体类里的属性要保持一致;
  • 相关阅读:
    poj1087最大流拆点
    3月15上午的函数练习
    3月15
    3月13上午
    3月13
    3月12
    break语句
    3月11
    3月10号
    3月9号
  • 原文地址:https://www.cnblogs.com/Sailsail/p/7681243.html
Copyright © 2020-2023  润新知