我正在开发一款应用程序,用于处理在该影院中运行的影院和电影。感谢Stack溢出,我已经成功解析了JSON文件!但我坚持在微调器上显示JSON ARRAY名称。
这是JSON文件。
{
"Abirami": [ //Theatre 1
{
"name": "Bairavaa - Tamil"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "Gautamiputra Satakarni - Telugu"
},
{
"name": "Khaidi No. 150 - Telugu"
}
],
"AGS_OMR": [ //Theatre 2
{
"name": "Gautamiputra Satakarni - Telugu"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
}
],
"AGS_TNagar": [ //Theatre 3
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
}
],
"AGS_Villivakkam": [
{
"name": "Bairavaa - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Khaidi No. 150 - Telugu"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
}
],
"INOX_Arcot_Road": [
{
"name": "Bairavaa - Tamil"
},
{
"name": "Dhuruvangal Pathinaaru - Tamil"
},
{
"name": "Gautamiputra Satakarni - Telugu"
},
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Khaidi No. 150 - Telugu"
}
],
"INOX_Citi_Centre": [
{
"name": "Dhuruvangal Pathinaaru - Tamil"
},
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "OK Jaanu - Hindi"
}
],
"LUXE": [
{
"name": "Dangal (Tamil) - Tamil"
},
{
"name": "Dangal - Hindi"
},
{
"name": "Live By Night - English"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Khaidi No. 150 - Telugu"
},
{
"name": "Gautamiputra Satakarni - Telugu"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "Shatamanam Bhavati - Telugu"
}
],
"mayajaal": [
{
"name": "Khaidi No. 150 - Telugu"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "xXx: Return Of Xander Cage (3D Tamil) - Tamil"
},
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "Kirik Party - Kannada"
},
{
"name": "Dangal - Hindi"
},
{
"name": "Shatamanam Bhavati - Telugu"
},
{
"name": "Haraamkhor - Hindi"
},
{
"name": "Gautamiputra Satakarni - Telugu"
}
],
"PVR_Ampa": [
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Khaidi No. 150 - Telugu"
},
{
"name": "Passengers (3D) - English"
},
{
"name": "Gautamiputra Satakarni - Telugu"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Live By Night - English"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "Dangal - Hindi"
},
{
"name": "Shatamanam Bhavati - Telugu"
}
],
"PVR_Velachery": [
{
"name": "Dhuruvangal Pathinaaru - Tamil"
},
{
"name": "Khaidi No. 150 - Telugu"
},
{
"name": "Bairavaa - Tamil"
},
{
"name": "OK Jaanu - Hindi"
},
{
"name": "Koditta Idangalai Nirappuga - Tamil"
},
{
"name": "xXx: Return Of Xander Cage (3D) - English"
},
{
"name": "Dangal - Hindi"
}
]
}
如何在旋转器中仅显示剧院名称,如剧院1,2,3等? JSON文件存在于Android Studio的Assets文件夹中。
编辑:从资产中解析JSON文件。
private ArrayList<String> getTheatres(String theatre_results) {
JSONArray jsonArray = null;
ArrayList<String> cList = new ArrayList<String>();
try {
InputStream is = getResources().getAssets().open(theatre_results);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
jsonArray = new JSONArray(json);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
cList.add(jsonArray.getJSONObject(i).getString("name"));
System.out.println("value is "+cList.get(0));
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException je) {
je.printStackTrace();
}
return cList;
}
答案 0 :(得分:0)
JSONObject rootJsonObject = new JSONObject(json);
Iterator iterator = rootJsonObject.keys();
while(iterator.hasNext()){
//this below key will be the names of your each theater, you may
//add it into a list to populate spinner
String key = (String)iterator.next();
JsonArray theaterArray = rootJsonObject.getJsonArray(key);
}
将所有键添加到列表后,您可以填充该列表中的微调器。我建议你使用更好的方法,制作像这样的哈希图
HashMap<String, JsonArray> = new HashMap();
将所有数据添加到其中,使用hashmap键填充您的微调器,并选择任何微调器项目,您可以从针对该特定键的hashmap获取所有内部数据(影院阵列)。
答案 1 :(得分:0)
尝试使用此代码获取所有影院名称
ArrayList<String> theaters = getTheaters(theatre_results);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,android.R.id.text1,theaters);
spinner.setAdapter(adapter);
private ArrayList<String> getTheatres(String theatre_results) {
JSONObject jsonObj = null;
try {
InputStream is = getResources().getAssets().open(theatre_results);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
jsonObject = new JSONObject(json);
return jsonObject.names();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException je) {
je.printStackTrace();
}
return cList;
}
使用此列表创建适配器并将其设置为微调器