首页 > 解决方案 > 如何从在线而不是资产文件夹中读取 Json

问题描述

我正在学习从在线制作测验应用程序,并且进展顺利。我想知道,与其从 assets 中读取 json ,不如从网上读取这样可以添加或相应更改问题并且用户不必更新 app.js 是明智的。

这是 JSON 结构。

{"questions" : [{"category":"general","question": "Grand Central Terminal, Park Avenue, New York is the world's", "choices": ["largest railway station","highest railway station","longest railway station","None of the above"], "correctAnswer":0},
    {"category":"science","question": "Entomology is the science that studies", "choices": ["Behavior of human beings","Insects","The origin and history of technical and scientific terms","the formation of rocks"], "correctAnswer":1},

    {"category":"science", "question":"What is known as the 'master gland' of the human body?", "choices":["Thyroid gland","Pituitary gland","Pineal gland","Pancreas"],"correctAnswer":1}
  ]}

从资产中读取的代码是

    private String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = mContext.getAssets().open("questionsJSON.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

我想在下一个问题加载时显示进度加载对话框,任何帮助将不胜感激。提前致谢。

标签: javaandroidjson

解决方案


最好的选择是使用REST API,从服务器/数据库获取数据,可以随时随地进行编辑

你可以学习使用Node js,它并不难,它是基于 JavaScript 的。

要从 API 获取 JSON,您可以使用Retrofit

如果您是初学者,学习和实施这些东西会有点困难,但从长远来看,这将是最佳选择

希望这有帮助!


推荐阅读