![](/img/trans.png)
[英]java.io.FileNotFoundException when I'm trying to read into a binary file in my Android Studio project
[英]“FileNotFoundException” when trying to read from JSON file in Android Studio
我正在尝试使用Android Studio中的JSON文件构造带有对象的列表。 JSON文件“ compliments.json”位于地图app / src / main / res中。
尝试读取此文件时,我不断收到“ java.io.FileNotFoundException”。 我尝试了很多类似主题的人建议的解决方案。 (我清除了缓存,将文件更改为另一张地图,并尝试了一种新的读取方式),但是它们似乎都不起作用。 这是我用来读取文件的代码:
public static ComplimentCatalog read(Context context) throws JSONException, IOException {
ComplimentCatalog ul = new ComplimentCatalog();
JsonParser parser = new JsonParser();
AssetManager assetManager = context.getAssets();
InputStream ims = assetManager.open("compliments.json");
Reader reader1 = new InputStreamReader(ims);
JsonElement rootElement = parser.parse(reader1);
JsonObject rootObject = rootElement.getAsJsonObject();
JsonArray uList = rootObject.getAsJsonArray("complimentList");
for(int i = 0; i < uList.size(); i++) {
JsonObject jsnComp = uList.get(i).getAsJsonObject();
ul.add(Compliment.read(jsnComp));
}
System.out.println("UL:" + ul.toString());
return ul;
}
这是json:
{
"complimentList":[
{
"text": "You look good today!"
},
{
"text": "Do you have a new T-Shirt?"
}
]
}
如果您需要任何其他文件/信息,我将很乐意提供它们。
如果要使用AssetManager assetManager = context.getAssets()
; 您的文件应放在app/src/main/assets/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.