以下是网络服务的网址:
http://watsmo.com/clients/techmate/getCity.php
它以下列格式返回JSON数据:
{
"Andhra Pradesh":{
"37":"Anantapur",
"48":"Bhimavaram",
"183":"Chittoor",
"191":"Eluru",
"52":"Guntur",
"50":"Kakinada",
"185":"Karnul",
"16":"Rajahmundry",
"182":"Tirupati",
"17":"Vijaywada",
"18":"Visakhapatnam",
"201":"Vizianagram"
},
"Bihar":{
"88":"Patna"
},
"Chhattisgarh":{
"163":"Bhillai",
"164":"Bilaspur",
"161":"Raipur"
},
"Delhi":{
"68":"Delhi",
"61":"New delhi"
},
"Gujarat":{
"6":"Ahmedabad",
"173":"Bhavnagar",
"172":"Jamnagar",
"136":"Rajkot",
"170":"Surat",
"8":"Vadodara"
}
}
此JSON包含州和相应城市的名称。
我想用两个微调器创建一个活动,第一个包含状态名称,另一个包含与第一个微调器中选择的状态相对应的城市名称。我不可能对此进行硬编码,我无法弄清楚如何使用Web服务执行此操作。请帮我解决一下这个。谢谢!
答案 0 :(得分:0)
为arrayList
创建一个全局变量 ArrayList<String> keysOfJson=new ArrayList();
现在,在获取jsonObject后,请查看是否有帮助
keysOfJson.clear();
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
if (jsonObject.get(key) instanceof JSONObject ) {
JSONObject object= jsonObject.getJSONObject(key);
if (!keysOfJson.contains(key)) {
keysOfJson.add(key);
}
}
}
答案 1 :(得分:0)
HashMap<int,ArrayList<String>> hmData=new HashMap<>();
for(int i=0;i<response.length();i++)
{
JSONObject jobj1=response.getJsonObject(i)
ArrayList<String> alcities=new ArrayList<>();
for(int j=0;j<jobj2.length()){
JSONObject jobj2=jobj1.getJsonObject(j);
alcities.add(jobj2.optString(jobj2));
}
hm.put(i,alcities);
//parse the cities from json object and store all cities.
}
注意:没有执行,但这可以给你一个粗略的想法。
答案 2 :(得分:0)
您可以从这个类似的[问题]中获取帮助:Extract JsonObject inbetween another JsonObject
选中此项以解决您的问题。
答案 3 :(得分:0)
可能会帮到你。获取不同的城市名称和州名列表以设置到微调器
public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
这两个方法用于将您的json响应转换为两个
的Map Object现在您可以分为状态数组和城市数组(CityId,城市名称)
private void makeJsonObjectRequest() {
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
Map<String, Object> stringObjectMap = jsonToMap(response);
ArrayList<String> statesName = new ArrayList<>();
ArrayList<City> finalcityArrayList= new ArrayList<>();
HashMap<String,String> mCityHashMap = new HashMap<>();
ArrayList<City> cityArrayList = new ArrayList<>();
for ( String key : stringObjectMap.keySet() ) {
System.out.println( key );
statesName.add(key);
mCityHashMap = ( HashMap<String,String>) stringObjectMap.get(key);
mCityHashMap.keySet().toArray();
mCityHashMap.values().toArray();
cityArrayList= new ArrayList<>();
HashMap<String,String> childCityHashMap = new HashMap<>();
for(String cityNumber: mCityHashMap.keySet()){
// childCityHashMap = (HashMap<String,String>) mCityHashMap.get(cityNumber);
City city = new City();
String strname = mCityHashMap.get(cityNumber);
Log.e("TAG","" + strname);
city.setId(cityNumber);
city.setName(mCityHashMap.get(cityNumber));
cityArrayList.add(city);
}
finalcityArrayList.addAll(cityArrayList);
}
Log.e("TAG","State Names "+statesName.size());
Log.e("TAG","City Names"+finalcityArrayList.size());
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
SpinnerApp.getInstance().addToRequestQueue(jsonObjReq);
}