简体   繁体   中英

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?

I want to use the value to populate spinner and set the key to the id of item selected from the spinner.

"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. I was told to convert JsonObject to 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": [ ... ] }

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:

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

Using the library, it will deliver your result filled in the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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