-1

我正在寻找如何使用 Klaxon 将我的 JSON 放入 LIstView 的数组中。这与 Java 不同,原因有两个。它是 Kotlin 和 Klaxon。

以下是一个州城镇的一系列拉链和信息。我正在通过 Fuel 提取它,我可以将它放入 Klaxon 以将部分对象读入文本字段,但我想构建一个列表,其中每个对象都有自己的行。想知道如何从 A 点喇叭进入列表 B 点。

不知道该怎么做。

{"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","纬度":"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"}zip":"02032","City":"East Walpole","State":"MA","lat":"42.1548","Long":"-71.2164"},{"zip":"02062" ,"城市":"诺伍德","州":"MA","lat":"42.1855","Long":"-71.2022"},{"zip":"02071","City":"南Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn","State": "MA","lat":"42.233","Long":"-71.3814"}zip":"02032","City":"East Walpole","State":"MA","lat":"42.1548","Long":"-71.2164"},{"zip":"02062" ,"城市":"诺伍德","州":"MA","lat":"42.1855","Long":"-71.2022"},{"zip":"02071","City":"南Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn","State": "MA","lat":"42.233","Long":"-71.3814"}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"}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"}City":"South Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn" "州":"MA","纬度":"42.233","长":"-71.3814"}City":"South Walpole","State":"MA","lat":"42.105","Long":"-71.2705"},{"zip":"01770","City":"Sherborn" "州":"MA","纬度":"42.233","长":"-71.3814"}

4

1 回答 1

0

您需要有一个对象来描述您的格式并使用您的库将其从 JSON 转换(不知道 Klaxon,但对于 GSON 将如示例中所示)

单独文件中的模型

public class Model {
public String zip, City, State, lat, Long;
}
public class ModeList {
public Model[] towns;
}

然后你需要你的价值观:

List<Model> listOfModel = new Gson().fromJson(yourJsonAsStringHere);

要在 ListView 中显示,您可以使用模型列表来获取元素的数量,并让每个元素显示一个值。

Klaxon 非常简单,您可以从 github https://github.com/cbeust/klaxon将示例检入自述文件中

使用 kotlin,您甚至不需要尽早构建类型。

于 2017-09-27T20:58:35.433 回答