简体   繁体   English

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

[英]How to read from a local JSON file in android studio and add to an ArrayList?

I am having difficulty reading from a local json file stored in my assets folder.我无法读取存储在我的资产文件夹中的本地 json 文件。

Here is the method I wrote to read from the file and add the objects the birdList ArrayList.这是我编写的从文件中读取并将对象添加到birdList ArrayList 的方法。

Where am I going wrong?我哪里错了?

 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();
        }
    }

Here is the json file I am reading from.这是我正在读取的 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. "
    }
  ]
}

Any help would be greatly appreciated任何帮助将不胜感激

Your json starts from {}, so firstly you should write你的 json 从 {} 开始,所以首先你应该写

JSONObject jsonobject = new JSONObject(jsonString);

Rest is ok休息没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM