0

I am getting below JSON response and I have to store both the data in spinner but when I select Alabama the "AL" in toast message should be used. How can I do this?

{
    "status" : "success",
    "data" : {
        "AL" : "Alabama",
        "AK" : "Alaska"
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Atul Dhanuka
  • 1,453
  • 5
  • 20
  • 56
  • Can you post the code you have and explain where do you find the problem that is blocking you? – Juan May 26 '17 at 11:06
  • Use custom adapter. check this link , which may help you https://stackoverflow.com/questions/35983176/how-to-create-spinner-list-using-customadapter-in-android – Nandakishore Shetty May 26 '17 at 11:08
  • Check out this answer about parsing JSON: https://stackoverflow.com/a/31993218/3050249 – Miha_x64 May 26 '17 at 11:17

3 Answers3

1

Try this my friend

JSONObject jsonObject = new JSONObject(response);
        Iterator<String> keys = jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            String value = jsonObject.getString(key);
            Log.v("**********", "**********");
            Log.v("category key", key);
            Log.v("category value", value);

            String firstChar = String.valueOf(value.charAt(0));

            if (firstChar.equalsIgnoreCase("{")) {
                JSONObject innerJObject = jsonObject.getJSONObject(key);
                Iterator<String> innerkeys = innerJObject.keys();
                String innerkey = innerkeys.next();
                String innervalue = innerJObject.getString(innerkey);
                Log.v("**********", "**********");
                Log.v("inner key", innerkey);
                Log.v("inner value", innervalue);
            }
        }
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

Hello Try this if it helps

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    List<City>cityList= new ArrayList<>();
    List<String> cityListName= new ArrayList<>();
    Spinner spnCity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spnCity=(Spinner)findViewById(R.id.spnCity);
        String response="{\"status\":\"success\",\"data\":{\"AL\":\"Alabama\",\"AK\":\"Alaska\"}}";
        try {
            JSONObject iObject=new JSONObject(response);
            JSONObject data=iObject.getJSONObject("data");
            Iterator<String> iter = data.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                try {
                    Object value = data.get(key);
                    cityListName.add(value.toString());
                    cityList.add(new City(key,value.toString()));
                } catch (JSONException e) {
                }
            }
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityListName);

            // Drop down layout style - list view with radio button
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            // attaching data adapter to spinner
            spnCity.setAdapter(dataAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String item = adapterView.getItemAtPosition(i).toString();
        for (int j = 0; j < cityList.size(); j++) {
            if(cityList.get(j).getCityName().equals(item)){
                Toast.makeText(adapterView.getContext(), "Selected: " + item +" "+cityList.get(j).getCityName(), Toast.LENGTH_LONG).show();
            }
        }
        // Showing selected spinner item

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
Pratik Vyas
  • 644
  • 7
  • 20
1

Try

JSONObject jsonObject = new JSONObject(jsonString);
String status = jsonObject.getString("status");
List<String> data = new ArrayList<String>();
JSONArray array = jsonObject.getJSONArray("data");
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
    String key = keys.next();
    String value = jsonObject.getString(key);
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53