简体   繁体   中英

How to call Json data using local variable?

I have created a Spinner list of string, which are same as the Json String. As like this:

Spinner list:

list.add("AED");list.add("ALL");list.add("AMD");list.add("ANG");list.add("AOA");list.add("ARS");

Json Data:

{"AED":4.065064,"AFN":86.705252,"ALL":121.591451,"AMD":525.502916,"ANG":1.94743,"AOA":404.690194,"ARS":62.08047}

I want to call Json data by selecting a item from my spinner list.

when I pass the string from my list Nothing happend.

protected void onPostExecute(final String result) {
            super.onPostExecute(result);

            try {
                final JSONObject jsonObject= new JSONObject(result);
                String currencyDetails= jsonObject.getString("rates");
                Log.i("Value is",currencyDetails);

                firstSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int first, long l) {

                        try {
                            String uniqueValue =jsonObject.getString(list.get(first));


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }

Could someone tell me, can I do what I want?

There is one mistake, on the line

 String uniqueValue = jsonObject.getString(list.get(first));

From the given JSON data, It has double value for every key-value pairs. You can see-

{"AED":4.065064,"AFN":86.705252,"ALL":121.591451,"AMD":525.502916,"ANG":1.94743,"AOA":404.690194,"ARS":62.08047}

So, Just get double value instead of string, like this-

String uniqueValue = jsonObject.getDouble(list.get(first));

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