Android解析sdcard下的json文件

bean

public abstract class BaseBean implements Serializable {
    @NonNull
    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        try {
            for (Field field : getClass().getDeclaredFields()) {
                field.setAccessible(true);
//                Log.d(getClass().getSimpleName(), "field name:" + field.getName() + " value:" + field.get(this));
                jsonObject.put(field.getName(), field.get(this));
            }

            return jsonObject.toString();
        } catch (IllegalAccessException | JSONException e) {
            e.printStackTrace();
        }

        return super.toString();
    }

}


public class CheckListItem extends BaseBean implements Comparable<CheckListItem> {
    public String name;

    public String result;

    public String para;

    public int type;

    public CheckListItem() {
    }

    public CheckListItem(String name, String result, String para, int type) {
        this.name = name;
        this.result = result;
        this.para = para;
        this.type = type;
    }

    public CheckListItem copy(@NonNull CheckListItem item) {
        this.name = item.name;
        this.result = item.result;
        this.para = item.para;
        this.type = item.type;
        return this;
    }

    @NonNull
    @Override
    public Object clone() {
        return new CheckListItem(this.name, this.result, this.para, this.type);
    }

    public CheckListItem(@NonNull JSONObject json) {
        name = json.optString(Constant.CHECK_LIST_TAG_NAME);
        result = json.optString(Constant.CHECK_LIST_TAG_RESULT);
        para = json.optString(Constant.CHECK_LIST_TAG_PARA);
    }

    @Override
    public int compareTo(CheckListItem item) {
        return type - item.type;
    }
}

解析工具类

public class JsonParser {
    public static List<CheckListItem> parserDefault(Context context) {
        try {
            File file = new File(Constant.SYSTEM_AUTOTEST_CONF);
            InputStream inputStream;
            if (file.exists()) {
                inputStream = new FileInputStream(file);
            } else {
                inputStream = context.getResources().openRawResource(R.raw.default_config);
            }
            return parserDefault(inputStream);
        } catch (IOException | Resources.NotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static List<CheckListItem> parserDefault(InputStream in) throws IOException {
        JsonReader reader = new JsonReader(new InputStreamReader(in));
        try {
            return readEntriesArray(reader);
        } finally {
            in.close();
            reader.close();
        }
    }

    private static List<CheckListItem> readEntriesArray(JsonReader reader) throws IOException {
        List<CheckListItem> entries = new CopyOnWriteArrayList<>();

        reader.beginArray();
        while (reader.hasNext()) {
            entries.add(readEntries(reader));
        }
        reader.endArray();
        return entries;
    }

    private static CheckListItem readEntries(JsonReader reader) throws IOException {
        String taskName = null;
        String result = null;
        String para = null;
        int type = -1;

        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            switch (name) {
                case Constant.CHECK_LIST_TAG_NAME:
                    taskName = reader.nextString();
                    break;
                case Constant.CHECK_LIST_TAG_RESULT:
                    result = reader.nextString();
                    break;
                case Constant.CHECK_LIST_TAG_PARA:
                    para = reader.nextString();
                    break;
                case Constant.CHECK_LIST_TAG_TYPE:
                    type = reader.nextInt();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        return new CheckListItem(taskName, result, para, type);
    }
}