Java 使用下面给定的json响应将列表视图拆分为多个部分
我有一个json响应,如下所示Java 使用下面给定的json响应将列表视图拆分为多个部分,java,android,json,Java,Android,Json,我有一个json响应,如下所示 { "continent":"Africa", "name":"Djezzy", "cid":"3", "country":"Algeria", "filename":"djezzy.png", "iso2":"DZ", "iso3":"DZA", "network":"OTA NET, ALG 02, 603 02" },
{
"continent":"Africa",
"name":"Djezzy",
"cid":"3",
"country":"Algeria",
"filename":"djezzy.png",
"iso2":"DZ",
"iso3":"DZA",
"network":"OTA NET, ALG 02, 603 02"
},
{
"continent":"Africa",
"name":"Etisalat Nigeria",
"cid":"156",
"country":"Nigeria",
"filename":"etisalat.png",
"iso2":"NG",
"iso3":"NGA",
"network":"Etisalat"
},
{ },
{
"continent":"Americas",
"name":"Tigo",
"cid":"47",
"country":"Colombia",
"filename":"tigo.png",
"iso2":"CO",
"iso3":"COL",
"network":"Tigo"
},
{
"continent":"Europe",
"name":"Beeline-Armenia",
"cid":"11",
"country":"Armenia",
"filename":"beeline.png",
"iso2":"AM",
"iso3":"ARM",
"network":"Beeline, ArmenTel, ARMGSM, ARM 01, 283 01"
},
{
"continent":"Europe",
"name":"life",
"cid":"220",
"country":"Ukraine",
"filename":"life.png",
"iso2":"UA",
"iso3":"UKR",
"network":"UA Astelit, UKR 06, 255 06"
},
{
"continent":"Europe",
"name":"T-Mobile",
"cid":"240",
"country":"Montenegro",
"filename":"tmobile.png",
"iso2":"ME",
"iso3":"MNE",
"network":"T-Mobile CG, YU 04, 220 04, 297 02, MNE 02"
},
{
"continent":"Europe",
"name":"Turkcell",
"cid":"215",
"country":"Turkey",
"filename":"turkcell.png",
"iso2":"TR",
"iso3":"TUR",
"network":"Turkcell"
},
{
"continent":"Middle East & Asia",
"name":"3hk",
"cid":"96",
"country":"Hong Kong",
"filename":"3hk.png",
"iso2":"HK",
"iso3":"HKG",
"network":"3, 3G, Orange, 3-Dualband"
}
现在,我需要显示国家列表,并将列表划分为各个洲的部分。我可以使用可扩展列表视图,但如何将此json划分为多个数组列表,然后在可扩展列表视图中将其用作组和子级 为了更好的理解,我需要像这样显示数据
提前谢谢。像这样试试,可能会对你有所帮助 初始化字符串数组
String[] CENTERNAME,CENTER_ID;
在获取json数组之前初始化arraylist
//Converting string to Array list
ArrayList<String> centername_arr= new ArrayList<String>();
ArrayList<String> Center_Id_arr= new ArrayList<String>();
if ((response.toString()).contains("{"))
{
// JSONArray jr = new JSONArray(response);
SoapObject rep = (SoapObject) envelope.bodyIn;
JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
for (int i = 0; i < jr.length(); i++)
{
JSONObject jb = (JSONObject) jr.get(i);
Center_id = jb.getString("CenterId");
CenterName = jb.getString("CenterName");
centername_arr.add(CenterName);
Center_Id_arr.add(Center_id);
}}
//Convert Arraylist to String Array
CENTERNAME = new String[centername_arr.size()];
CENTERNAME = centername_arr.toArray(CENTERNAME);
CENTER_ID= new String[Center_Id_arr.size()];
CENTER_ID = Center_Id_arr.toArray(CENTER_ID);
//将字符串转换为数组列表
ArrayList centername_arr=新建ArrayList();
ArrayList Center_Id_arr=新建ArrayList();
if((response.toString())包含(“{”))
{
//JSONArray jr=新JSONArray(响应);
SoapObject rep=(SoapObject)envelope.bodyIn;
JSONArray jr=新的JSONArray(rep.getpropertyastring(0));
对于(int i=0;i
然后您可以在Listview中使用字符串数组谢谢大家花时间阅读我的问题..我找到了答案,下面是解决方案
JSONObject j=jo.getJSONObject("response");
JSONArray jar=j.getJSONArray("data");
for (int i = 0; i < jar.length(); i++) {
JSONObject job=jar.getJSONObject(i);
String continent=job.getString("continent");
String name=job.getString("name");
String country=job.getString("country");
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
boolean exists = false;
for (HashMap<String, String> hashMap : contactList) {
try {
if (hashMap.get("continent").equals(continent)) {
exists = true;
break;
}
} catch (Exception e) {
}
}
if (!exists) {
contact.put("continent", continent);
}
contact.put("name", name);
contact.put("country", country);
contactList.add(contact);
}
JSONObject j=jo.getJSONObject(“响应”);
JSONArray jar=j.getJSONArray(“数据”);
for(int i=0;i
您是否尝试在谷歌上搜索JSON解析库???只需检查您的JSON。我正在向任何在线JSON验证器或格式化程序检查它,但它不正确您有JSON对象,现在您可以创建一个getter setter并将其添加到arraylist。使用它您可以将数据数组显示到listview中。它应该是jsonArray……缺少“[”和“]'分别在开始和结束时,我现在需要显示国家列表并将列表划分为各个洲的部分。我可以使用可扩展列表视图,但如何将此json划分为多个数组列表,然后在可扩展列表视图中用作组和子组。非常感谢Mano抽出时间帮助我出局。我得到了解决方案…:)如果我的答案对你有帮助,就接受答案:-)我本来想给你投票,但不幸的是,投票需要15个声誉,而我只有6个…我想再次感谢你@Mano…:)没关系,当你获得声誉时…:)