在Java Android中加载外部JSON文件

在Java Android中加载外部JSON文件,java,android,json,sqlite,Java,Android,Json,Sqlite,我刚开始学习Java。我目前正在开发一个从.JSON文件检索数据并将其存储到SQLite数据库的小应用程序 我试图打开一个.JSON文件并将其存储为JSONObject。下面是我读取JSON文件的代码 private JSONObject readFile(String path) { StringBuilder sb = new StringBuilder(); try { FileReader in = new FileReader(new File(path

我刚开始学习Java。我目前正在开发一个从.JSON文件检索数据并将其存储到SQLite数据库的小应用程序

我试图打开一个.JSON文件并将其存储为JSONObject。下面是我读取JSON文件的代码

private JSONObject readFile(String path) {
    StringBuilder sb = new StringBuilder();
    try {
        FileReader in = new FileReader(new File(path));
        int b;
        while((b = in.read()) != -1) {
            sb.appendCodePoint(b);
        }
        in.close();

        return new JSONObject(sb.toString());
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}
我的JSON文件存储在:app\src\main\java\proj\bb\database\jsonFiles 我正在调用readFile方法,如下所示:,但它会抛出一个空指针。DatabaseOperations是类名

JSONObject jsonFile = readFile(DatabaseOperations.class.getResource("filename.json").toString());
JSONArray arr = jsonFile.getJSONArray("someArray");
我也尝试过,但没有成功:

JSONObject jsonFile = readFile("jsonFiles/filename.json").toString());
JSONArray arr = jsonFile.getJSONArray("someArray");
所以我的问题是,我需要通过什么样的途径作为论点?还是我做错了什么


提前谢谢

将JSON文件放在资产文件夹中,然后从那里访问它。 您可以使用以下方法将数据读取为-

public static String getJSONData(Context context, String textFileName) {
    String strJSON;
    StringBuilder buf = new StringBuilder();
    InputStream json;
    try {
        json = context.getAssets().open(textFileName);

        BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));

        while ((strJSON = in.readLine()) != null) {
            buf.append(strJSON);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return buf.toString();
}
方法将返回JSON字符串,您可以将JSONObject的实例创建为

外部json文件为:sample.json

String jsonString = getJSONData(MainActivity.this, "sample.json");

JSONObject jsonObject = new JSONObject(jsonString);

将JSON文件放在assets文件夹中,然后从那里访问它。 您可以使用以下方法将数据读取为-

public static String getJSONData(Context context, String textFileName) {
    String strJSON;
    StringBuilder buf = new StringBuilder();
    InputStream json;
    try {
        json = context.getAssets().open(textFileName);

        BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));

        while ((strJSON = in.readLine()) != null) {
            buf.append(strJSON);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return buf.toString();
}
方法将返回JSON字符串,您可以将JSONObject的实例创建为

外部json文件为:sample.json

String jsonString = getJSONData(MainActivity.this, "sample.json");

JSONObject jsonObject = new JSONObject(jsonString);

JSONObject profile=新的JSONObject YourString;是的,但是字符串必须是什么…?JSONObject profile=newJSONObject yourString;是的,但你的绳子是什么…?你是个英雄。我尝试了很多东西,效果非常好,谢谢!你是个英雄。我尝试了很多东西,效果非常好,谢谢!