【发布时间】:2016-10-29 21:30:09
【问题描述】:
我有一个像这样的原始 json 文件
{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
{"_id":1283378,"name":"Gorkhā","country":"NP","coord":{"lon":84.633331,"lat":28}}
{"_id":1270260,"name":"State of Haryāna","country":"IN","coord":{"lon":76,"lat":29}}
{"_id":708546,"name":"Holubynka","country":"UA","coord":{"lon":33.900002,"lat":44.599998}}
{"_id":1283710,"name":"Bāgmatī Zone","country":"NP","coord":{"lon":85.416664,"lat":28}}
{"_id":529334,"name":"Mar’ina Roshcha","country":"RU","coord":{"lon":37.611111,"lat":55.796391}}
{"_id":1269750,"name":"Republic of India","country":"IN","coord":{"lon":77,"lat":20}}
{"_id":1283240,"name":"Kathmandu","country":"NP","coord":{"lon":85.316666,"lat":27.716667}}
{"_id":703363,"name":"Laspi","country":"UA","coord":{"lon":33.733334,"lat":44.416668}}
它不是一个 JSON 数组——它是一个巨大的 JSON 对象列表。这里Populate the spinner from JSON In Android,这里Convert json array to list of json(raw) strings 这里android how to convert json array to string array 甚至这里https://developer.android.com/guide/topics/ui/controls/spinner.html 假设我有一个JSON 数组或静态数据。我想要两个微调器,一个用于乡村,第二个用于城市。假设我有国家/地区列表,因此它将是静态数据。我希望第二个微调器从原始 json 文件中动态加载与第一个微调器中所选城市相对应的所有城市。我认为我可以处理微调器的事情,但是如何将原始 json 文件中的数据加载到微调器中?我是否需要修改这个原始文件,使其成为一个 json 数组,然后执行以下操作:
JSONArray jsonArray = new JSONArray(arraytitle);
List<String> list = new ArrayList<String());
for(int i = 0 ; i < jsonArray.length();i++){
list.add(jsonArray.getJSONObject(i));
}
编辑 我找到了这个答案https://stackoverflow.com/a/26780581/4671908 - 如果我成功了,我会发布一个答案
【问题讨论】:
标签: android arrays json android-studio spinner