关于省市的多级联动问题

写了个测试 使用gson进行数据的解析  代码如下:
String[] array = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z" };
jsonText = "{\"B\":{\"rows\":[{\"id\":2,\"text\":\"北京市\"}]},\"G\":{\"rows\":[{\"id\":1988,\"text\":\"广州市\"},{\"id\":1958,\"text\":\"深圳\"}]},\"Q\":{\"rows\":[{\"id\":1373,\"text\":\"青岛市\"}]},\"T\":{\"rows\":[{\"id\":23,\"text\":\"天津市\"}]}}";
try {
List<City> mCityLst = new ArrayList<ActivityMain.City>();
Gson gson = new Gson();
JSONObject jsonObj = new JSONObject(jsonText);
for (int i = 0; i < array.length; i++) {
String key = array[i];
boolean isHas = jsonObj.has(key);
if (!isHas) {
System.out.println("没有字母:" + key);
continue;
}
String value = jsonObj.getString(key);
System.out.println("当前字母:" + key + "____" + value);
JSONObject jObj = new JSONObject(value);
String mVlaue = jObj.getString("rows");
List<City> cityLst = gson.fromJson(mVlaue,
new TypeToken<List<City>>() {
}.getType());
for (int j = 0; j < cityLst.size(); j++) {
City item = cityLst.get(j);
item.indexFlag = key;
System.out.println("开始累加:" + key);
mCityLst.add(item);
}
}
for (int i = 0; i < mCityLst.size(); i++) {
City city = mCityLst.get(i);
System.out.println("相关信息:" + city.id + "___" + city.text
+ "___" + city.indexFlag);
}
} catch (JSONException e) {
e.printStackTrace();
}

猜你喜欢

转载自lenzol.iteye.com/blog/1821315