如何将此JsonObject
映射到JsonArray
以获取密钥和值?
我想使用该值来填充微调器并将键设置为从微调器中选择的项的id。
"data": {
"214": "FIRST CITY MONUMENT BANK PLC",
"215": "UNITY BANK PLC",
"221": "STANBIC IBTC BANK PLC",
"232": "STERLING BANK PLC",
"301": "JAIZ BANK",
"304": "Stanbic Mobile",
"305": "PAYCOM",
"307": "Ecobank Mobile",
"309": "FBN MOBILE",
"311": "Parkway",
"315": "GTBank Mobile Money",
"322": "ZENITH Mobile",
"323": "ACCESS MOBILE",
"401": "Aso Savings and Loans",
"044": "ACCESS BANK NIGERIA",
"014": "AFRIBANK NIGERIA PLC",
"063": "DIAMOND BANK PLC",
"050": "ECOBANK NIGERIA PLC",
"084": "ENTERPRISE BANK LIMITED",
"070": "FIDELITY BANK PLC",
"011": "FIRST BANK PLC",
"058": "GTBANK PLC",
"030": "HERITAGE BANK",
"082": "KEYSTONE BANK PLC",
"076": "SKYE BANK PLC",
"068": "STANDARD CHARTERED BANK NIGERIA LIMITED",
"032": "UNION BANK OF NIGERIA PLC",
"033": "UNITED BANK FOR AFRICA PLC",
"035": "WEMA BANK PLC",
"057": "ZENITH BANK PLC"
}
这是我的代码,但它不起作用。我正在使用谷歌的排球图书馆。我被告知要将JsonObject
转换为JsonArray
。
private void getData() {
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, serverUrl_list,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray obj = response.getJSONArray("Data");
for (int k=0; k<obj.length(); k++) {
while (mapIterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) mapIterator.next();
Integer keyValue = (Integer) mapEntry.getKey();
String value = (String) mapEntry.getValue();
//iterate over the array and print each value
for (int i=0; i<mapEntry.; i++) {
System.out.print(value[i] + " ");
}
System.out.println();
}
}
students.add(obj.getString("Data"));
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, students);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Bank.setAdapter(spinnerAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(obreq);
}
答案 0 :(得分:0)
您无法从JSON获取数组,因为它是{"data": { ... }}
而不是{"data": [ ... ] }
你有一个对象,你需要迭代键
private void getData() {
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bank.setAdapter(spinnerAdapter);
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, serverUrl_list,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("Data");
Iterator<String> keys = obj.keys();
while(keys.hasNext()) {
String v = obj.optString(keys.next());
spinnerAdapter.add(v);
}
} catch ( ) { }
}
},
答案 1 :(得分:0)
查看我们一直在研究的这个library,它也基于凌空。该库本身不会解析数据。您所要做的就是创建一个包含称为数据的地图字段的对象,如下所示:
public class Model {
private Map<String, String> data;
}
使用该库,它将提供填写在地图中的结果。