我在raw
中创建了一个res
Android资源目录,并向其添加了cars.json
。它显示在我的包浏览器上。当我试着打电话
File file = new File("raw/cars.json")
我收到FileNotFoundException。我尝试过简单地使用cars.json甚至是文件的整个路径,但我得到了相同的结果。
有什么建议吗?
答案 0 :(得分:3)
要从原始目录获取资源,请致电getResources().openRawResource(resourceName)
。
这会给你一个InputStream
答案 1 :(得分:1)
您可以直接使用此功能:
private String getJsonStringFromRaw(filenmae)
{
String jsonString = null;
try {
InputStream inputStream= getAssets().open(filename);
int size = inputStream.available();
byte[] buffer= new byte[size];
inputStream.read(buffer);
inputStream.close();
jsonString = new String(buffer, "UTF-8"); }
catch (IOException e)
{
e.printStackTrace();
return null;
}
return jsonString;
}
您可以通过迭代下面的json对象来访问json数据。 JSONObject obj = new JSONObject(getJsonStringFronRaw(" cars.json"))
答案 2 :(得分:-1)
你好, 我在这个会话中使用了InputStream.Dontwork for yourrs。
InputStream myInput=getResources().openRawResource(R.raw.your_json);
然后你可以使用任何where.it如此简单