Java 在Android Studio中尝试读取JSON文件时出现“FileNotFoundException”
我试图在Android Studio中用JSON文件中的对象构建一个列表。 JSON文件compaids.JSON位于map app/src/main/res中 当我试图读取这个文件时,我一直得到一个java.io.FileNotFoundException。我已经尝试过很多类似线程中的人建议的解决方案。我清除了缓存,将文件更改为另一个地图,并尝试了一种新的读取方式,但似乎都不起作用。以下是我在文件中读取的代码:Java 在Android Studio中尝试读取JSON文件时出现“FileNotFoundException”,java,android-studio,Java,Android Studio,我试图在Android Studio中用JSON文件中的对象构建一个列表。 JSON文件compaids.JSON位于map app/src/main/res中 当我试图读取这个文件时,我一直得到一个java.io.FileNotFoundException。我已经尝试过很多类似线程中的人建议的解决方案。我清除了缓存,将文件更改为另一个地图,并尝试了一种新的读取方式,但似乎都不起作用。以下是我在文件中读取的代码: public static ComplimentCatalog read(Cont
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;
}
如果您需要任何其他文件/信息,我很乐意提供。如果您要使用AssetManager AssetManager=context.getAssets;您的文件应该放在app/src/main/assets/中。您应该创建一个名为assets的新文件夹,并将compaids.json放在该文件夹中 AssetManager将查找资产文件夹中的文件
{
"complimentList":[
{
"text": "You look good today!"
},
{
"text": "Do you have a new T-Shirt?"
}
]
}