开发者社区> 问答> 正文

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

我很难读取存储在资产文件夹中的本地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. "
    }
  ]
}

任何帮助将不胜感激

展开
收起
Puppet 2019-12-04 09:58:33 3057 0
1 条回答
写回答
取消 提交回答
  • JSONArray jsonArray = new JSONArray(jsonString);

    修改成 String data = jsonString.getString("birds"); JSONArray jsonArray = JSONArray.fromObject(data);

    2019-12-13 14:48:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载