简体   繁体   English

如何在Java中将json对象映射到jsonarray?

[英]How to map json object to jsonarray in Java?

How can I map this JsonObject to JsonArray in order to get the key and value? 我如何将此JsonObject映射到JsonArray以获得键和值?

I want to use the value to populate spinner and set the key to the id of item selected from the spinner. 我想使用该值填充微调器并将键设置为从微调器中选择的项目的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"
}

This my code but it's not working. 这是我的代码,但是不起作用。 I am using google's Volley library. 我正在使用Google的Volley库。 I was told to convert JsonObject to JsonArray . 有人告诉我要将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);
}

You can't get an array from your JSON, since it is {"data": { ... }} and not {"data": [ ... ] } 您无法从JSON获取数组,因为它是{"data": { ... }}而不是{"data": [ ... ] }

You have an object, and you need to iterate over the keys 您有一个对象,并且需要遍历键

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 ( ) {  }
                }
             },

Check out this library we've been working on, it's based on volley as well. 看看我们一直在努力的这个 ,它也是基于凌空的。 Instead of parsing the data, the library will do so itself. 库将自行解析数据,而不是解析数据。 All you have to do is create an object that contains a map field called data like so: 您所要做的就是创建一个对象,其中包含一个名为data的地图字段,如下所示:

public class Model {
   private Map<String, String> data;
}

Using the library, it will deliver your result filled in the map. 使用该库,它将把您的结果填入地图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM