java - 如何从 android studio 中的本地 JSON 文件读取并添加到 ArrayList 中?

标签 java android json

我在读取存储在 Assets 文件夹中的本地 json 文件时遇到困难。

这是我编写的从文件读取并将对象添加到birdList ArrayList 的方法。

我哪里出错了?

 public void getJSON() {
        String jsonString;
        try {
            InputStream inputStream = getAssets().open("birds.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();

            jsonString = new String(buffer, "UTF-8");
            JSONArray jsonArray = new JSONArray(jsonString);
            Log.e("MainActivity", "The json is: " + jsonString);

            for(int i = 0; i < jsonArray.length();i++){
                JSONObject obj = jsonArray.getJSONObject(i);
                Bird bird = new Bird();
                bird.setScientificName(obj.getString("scientific_name"));
                bird.setCommonName(obj.getString("common_name"));
                bird.setNumberImage(obj.getString("image"));
                bird.setDescription(obj.getString("description"));
                birdList.add(bird);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我正在读取的 json 文件。

{
  "birds": [
    {
      "scientific_name": "Cyanocitta stelleri",
      "common_name": "Steller's jay",
      "image": "stellers_jay",
      "description": "The Steller's jay is a jay native to western North America, closely related to the blue jay found in the rest of the continent, but with a black head and upper body. It is also known as the long-crested jay, mountain jay, Braham's jay and pine jay. It is the only crested jay west of the Rocky Mountains."
    },
    {
      "scientific_name": "Tachycineta thalassina",
      "common_name": "Violet-green swallow",
      "image": "violet_green_swallow",
      "description": "The violet-green swallow is a small North American passerine bird in the swallow family. These aerial insectivores are distributed along the west coast from Alaska to Mexico, extending as far east as Montana and Texas. With an appearance very similar to the tree swallow, these individuals can be identified by the white rump side-patches that appear to separate their green back and purple tail. "
    },
    {
      "scientific_name": "Turdus migratorius",
      "common_name": "American robin",
      "image": "american_robin",
      "description": "The American robin is a migratory songbird of the true thrush genus and Turdidae, the wider thrush family. It is named after the European robin[2] because of its reddish-orange breast, though the two species are not closely related, with the European robin belonging to the Old World flycatcher family. The American robin is widely distributed throughout North America, wintering from southern Canada to central Mexico and along the Pacific Coast. "
    }
  ]
}

任何帮助将不胜感激

最佳答案

你的json从{}开始,所以首先你应该写

JSONObject jsonobject = new JSONObject(jsonString);

休息就好

关于java - 如何从 android studio 中的本地 JSON 文件读取并添加到 ArrayList 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147750/

相关文章:

json - 将多个变量而不是文件传递给 jq 命令

java - ImageJ 叠加 ROI 缩放

java - 来自 JAVA 的 Mongo Replicaset 配置

java - "NoClassDefFoundError: javax/inject/Provider"即使包含 javax.inject 依赖项

Android布局,如何定位两个图像和一个搜索栏

android - Material 设计库需要 oldnineandroids 2.4.+ : build failed

javascript - 使用 JSON 响应

java - 通过 Jetty 9 Maven 插件中的 jetty.xml 设置队列限制

android - 如何在 Android 中制作粘性部分标题(如 iOS)?

python - 如何使用 python 格式化 JSON 样式文本?