There is such a JSON file, is it possible to somehow get a specific element from this file by id?

{ "items": [ { "id": "1", "title": "Заголовок 1", "author": "Автор 1", "source": "(null)", "text": "Текст 1", "favorite": "0", "filename": "file_1", "image": "file_1", "category": "Категория" }, { "id": "2", "title": "Заголовок 2", "author": "Автор 2", "source": "(null)", "text": "Текст 2", "favorite": "0", "filename": "file_2", "image": "file_2", "category": "Категория" }, { "id": "3", "title": "Заголовок 3", "author": "Автор 3", "source": "(null)", "text": "Текст 3", "favorite": "0", "filename": "file_3", "image": "file_3", "category": "Категория" } ] } 

There is a class that describes the element.

Item

 public class Item implements { @SerializedName("id") @Expose private int id; @SerializedName("title") @Expose private String title; @SerializedName("author") @Expose private String author; @SerializedName("source") @Expose private String source; @SerializedName("text") @Expose private String text; @SerializedName("favorite") @Expose private String favorite; @SerializedName("filename") @Expose private String filename; @SerializedName("image") @Expose private String image; @SerializedName("category") @Expose private String category; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getFavorite() { return favorite; } public void setFavorite(String favorite) { this.favorite = favorite; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } } 

Itemlist

 public class ItemList { @SerializedName("items") @Expose private List<Item> items = null; public List<Item> getItems() { return items; } public void setItems(List<Item> items) { this.items = items; } } 

All objects get like this:

 private void loadData() { try { InputStream json = getAssets().open("items.json"); itemList = new Gson().fromJson(new InputStreamReader(json, "UTF-8"), ItemList.class); myAdapter = new MyAdapter(itemList.getItems(), MainActivity.this); rvList.setAdapter(MyAdapter); } catch (IOException e) { e.printStackTrace(); } } 
  • It is possible, but in any case you need to parse the entire file (string) (or at least to the desired item - I don’t remember, json has stream parsers) and then just pull it out of the list: itemList.getItems (). GetId (id) . There is another option to look in the file for the key and get regular with the regulars, but you need nothing and so you just lose. - Valeriy
  • I realized thanks. I now needed to know if there is any other way of not choosing everything in the list to get the right item right away. - Natalia Sergeevna

0