1

I got this code from a link here, so how do I get this to output from a JSON source URL?

This is the generated code data:

ArrayList<ItemModel> items = new ArrayList<>();

items.add(new ItemModel("10.30", "120/10","80","Tue,31 Oct 17"));
items.add(new ItemModel("10.30", "142/95","95","Tue,31 Oct 17"));
items.add(new ItemModel("15.30", "120/95","200","Tue,31 Oct 17"));
items.add(new ItemModel("20.30", "120/10","80","Tue,29 Oct 17"));
items.add(new ItemModel("10.30", "120/10","50","Tue,29 Oct 17"));

items.add(new ItemModel("10.30", "140/10","80","Tue,28 Oct 17"));
items.add(new ItemModel("10.30", "30/75","40","Tue,28 Oct 17"));
items.add(new ItemModel("10.30", "150/80","70","Tue,28 Oct 17"));

return  items;

So, I want that output from a JSON url or Loop in Item.add(new ItemModel)?

Note: if needed, here is the JSON source link

JuniorCode
  • 11
  • 2
  • 1
    you need to first get the json from the url and then parse it using using json parser. you can use any netowrking library for downloading json and using any parsing library for parsing json. you can just google them – Raghunandan Jun 26 '21 at 16:03
  • I got to parser JSON, but ho to implement sample code to item.add(new ItemModel()); ?? – JuniorCode Jun 26 '21 at 16:16
  • if you use parser like gson you don't need to add items to list. the json is converted to pojo classes in case of java and in case of kotlin it will be data classes. you could also use moshi. – Raghunandan Jun 27 '21 at 03:49

1 Answers1

0

You can create custom model class which match with your JSON data. And use library Gson you can easily parse response to list object.

class MyModel {
    String name, realname, team, ...;
}

Gson gson = new Gson();
yourListData = gson.fromJson(jsonData, new TypeToken<List<MyModel>>(){}.getType());

Gson library: https://github.com/google/gson

rasfarrf5
  • 219
  • 1
  • 5