如何使用局部变量调用Json数据?

时间:2019-09-13 03:28:21

标签: json local-variables

我创建了一个Spinner字符串列表,该列表与Json String相同。像这样:

纺纱厂名单:

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

Json数据:

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

我想通过从微调器列表中选择一个项目来调用Json数据。

当我从列表中传递字符串时,什么都没发生。

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();
                        }


                    }

有人可以告诉我,我可以做我想做的事吗?

1 个答案:

答案 0 :(得分:0)

在线上有一个错误

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

从给定的JSON数据中,每个键值对都具有双精度值。您可以看到-

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

因此,只需获取双精度值而不是字符串,就像这样-

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