I'm looking on how to use Klaxon to get my JSON into an Array for a LIstView. This is different then Java for two reasons. It is Kotlin and Klaxon.
Below is a array of zips and info for towns in a state. I'm pulling it via Fuel and I can get it into Klaxon to read parts of the objects into text fields but I want to build a list where each object has its own row. Was wondering how to get from point A klaxon into the List point B.
Not sure how to go about this.
{"towns":[{"zip":"02052","City":"Medfield","State":"MA","lat":"42.178","Long":"-71.3059"},{"zip":"02054","City":"Millis","State":"MA","lat":"42.1677","Long":"-71.3601"},{"zip":"02081","City":"Walpole","State":"MA","lat":"42.1429","Long":"-71.2571"},{"zip":"02030","City":"Dover","State":"MA","lat":"42.2341","Long":"-71.2861"},{"zip":"02056","City":"Norfolk","State":"MA","lat":"42.1185","Long":"-71.3287"},{"zip":"02032","City":"East Walpole","State":"MA","lat":"42.1548","Long":"-71.2164"},{"zip":"02062","City":"Norwood","State":"MA","lat":"42.1855","Long":"-71.2022"},{"zip":"02071","City":"South Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn","State":"MA","lat":"42.233","Long":"-71.3814"}
You need to have a object that describes your format and use your library to convert it from JSON (dont know about Klaxon but for GSON would be as show in sample)
models in separated files
public class Model {
public String zip, City, State, lat, Long;
}
public class ModeList {
public Model[] towns;
}
Then where you need your values:
List<Model> listOfModel = new Gson().fromJson(yourJsonAsStringHere);
To display in a ListView you use your list of Model to get the number of elements and each element to display one value.
Klaxon is pretty straight-forward you can check the samples into the README from the github https://github.com/cbeust/klaxon
With kotlin you even dont need to build types early.