Android 使用Gson解析Asset 目录下的"

2018-01-10  本文已影响405人  Cloverss

配合Rxjava,直接代码

Observable.create((ObservableOnSubscribe<String>) e -> {
            StringBuilder stringBuilder = new StringBuilder();
            InputStream inputStream = getResources().getAssets().open("techscan.json");
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(isr);
            String jsonLine;
            while ((jsonLine = reader.readLine()) != null) {
                stringBuilder.append(jsonLine);
            }
            reader.close();
            isr.close();
            inputStream.close();
            e.onNext(stringBuilder.toString());
            e.onComplete();
        }).map(s -> {
            List<GridConfig> list = new ArrayList<>(4096);
            GridConfig gridConfig;
            JsonArray elements = new JsonParser().parse(s).getAsJsonArray();
            for (int i = 0, size = elements.size(); i < size; i++) {
                gridConfig = C.GSON.fromJson(elements.get(i).getAsJsonObject(), GridConfig.class);
                list.add(gridConfig);
            }
            Log.d("TAG", "list.size: " + list.size());
            return list;
        }).map(list -> {
            BriteDatabase.Transaction transaction = SqlBriteDatabase.getDb().newTransaction();
            for (GridConfig gridConfig : list) {
                SqlBriteDatabase.insert(GridConfigDao.TABLE_NAME, new GridConfigDao.Builder(gridConfig).build());
            }
            transaction.markSuccessful();
            transaction.end();
            return true;
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Boolean>() {

                    QMUITipDialog tipDialog;

                    @Override
                    public void onSubscribe(Disposable d) {
                        tipDialog = DialogUtils.digLoading(activity, "正在载入配置项");
                        tipDialog.show();
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            ToastUtils.showShortToast("载入完成");
                        } else {
                            ToastUtils.showShortToast("载入失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        DialogUtils.dimiss(tipDialog);
                    }

                    @Override
                    public void onComplete() {
                        DialogUtils.dimiss(tipDialog);
                    }
                });

其实根本问题就是把.json文件中的字符读出来,然后想干什么就可以干什么

.json文件字符代码

            StringBuilder stringBuilder = new StringBuilder();
            InputStream inputStream = getResources().getAssets().open("techscan.json");
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(isr);
            String jsonLine;
            while ((jsonLine = reader.readLine()) != null) {
                stringBuilder.append(jsonLine);
            }
            reader.close();
            isr.close();
            inputStream.close();
            String result =  stringBuilder .toString();
上一篇下一篇

猜你喜欢

热点阅读