[英]Android Studio having trouble reading a JSONArray
I'm a beginner to Android Studio and I'm just learning how to read in JSON files.我是 Android Studio 的初学者,我只是在学习如何阅读 JSON 文件。 I used logcat to find out that I'm getting an exception on the line declaraing the JSONArray dataList.
我使用 logcat 发现我在声明 JSONArray dataList 的行上遇到异常。 My code is below for reference.
我的代码如下供参考。
try{
JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
JSONArray dataList = jsonObject.getJSONArray("eateries");
//Log.d("test", String.valueOf(dataList.length()));
for(int i = 0; i < dataList.length(); i++){
String EateryName = dataList.getJSONObject(i).getString("name");
EateryList.add(EateryName);
}
}catch (JSONException e){
e.printStackTrace();
//Log.d("test", "exception");
}
Follow these steps to solve your issue.请按照以下步骤解决您的问题。
Check GrIsHu answer simply you have to do is create assests folder in root of project只需检查GrIsHu 的答案,您只需在项目的根目录中创建assests文件夹
Here is the original Link这是 原始链接
String myJsonFileString = Utils.getJsonFromAssets(getApplicationContext(), "your.json");
Log.i("My data", jsonFileString);
Gson gson = new Gson();
Type listUserType = new TypeToken<List<User>>() { }.getType();
List<User> users_ = gson.fromJson(myJsonFileString, listUserType);
for (int i = 0; i < users.size(); i++) {
Log.i("data", "> Item " + i + "\n" + users.get(i));
}
go through original post go通过原帖
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.