【问题标题】:How to read json with this format如何读取这种格式的 json
【发布时间】:2014-08-04 01:52:40
【问题描述】:

我如何读取这种 JSON 格式,并且用户可以将 from 和 to 传递到 URL, 并希望在单击按钮时将这些 from_amount 和 to_amount 打印到文本视图中。

api url

{
  "from": "INR",
  "to": "GBP",
  "from_amount": 2,
  "to_amount": 0.019798718798321
}

我尝试执行下面的 sn-p。但它没有返回任何东西,也没有在 thr log cat 中给出任何错误

 private static final String API_URL = "http://devel.farebookings.com/api/curconversor/t1/t2/usds/json";
    if (!usdValue.getText().toString().equals("")) {
                        AsyncHttpClient client = new AsyncHttpClient();

                        client.get(API_URL, new AsyncHttpResponseHandler() {


                            @Override
                            public void onSuccess(String response) {
                                Log.i("CHACHING", "HTTP Sucess");

                                try {
                                    JSONObject jsonObj = new JSONObject(response);
                                    JSONObject ratesObject = jsonObj
                                            .getJSONObject("from");
                                    JSONObject ratessObject = jsonObj
                                            .getJSONObject("to");

                                    JSONObject rates2Object = jsonObj
                                            .getJSONObject("from_amount");
                                    //Double RateTo = Double.valueOf(t2
                                        //  .toString());

                                    Double RateFrom = Double.valueOf(t1
                                            .toString());
                                //  Double RateFrom = ratesObject.getDouble("from");
                                    //Double RateTo = ratesObject.getDouble(t2.toString());
                                    //Log.i("CHACHING", "GBP: " + gbpRate);
                                    //Log.i("CHACHING", "EUR: " + eurRate);

                                    Double usds = Double.valueOf(usdValue.getText()
                                            .toString());

                                    //Double gbps = usds * RateFrom;
                                //  Double euros = usds * RateTo;
                                    //Result.setText("ConvertedFrom: "
                                        //  + String.valueOf(RateFrom)+ "ConvertedTo: "
                                                //  + String.valueOf(RateTo));
                                    Toast.makeText(getApplicationContext(),
                                            Result.getText().toString(), Toast.LENGTH_LONG)
                                            .show();

                                    //ConvertedTo.setText("ConvertedTo: "
                                            //+ String.valueOf(RateTo));

                                } 

【问题讨论】:

  • 你尝试了什么?你有什么问题?什么语言?
  • @SLaks 我更新我的问题
  • 你有什么问题?输出日志?
  • @BlaShadow 我不知道如何在 url 中传递用户的选择,以便从 api.above 代码中读取特定数据,为我提供整个 json 数据。但我希望用户从中选择值下拉菜单和该特定值的结果

标签: java android json


【解决方案1】:

不要使用getJsonObject()方法来获取JSONObject里面的值,而是使用对应的getter来获取值的类型。键值对本身不是 JSONObject。

 JSONObject jsonObj = new JSONObject(response);

 String fromCurrency = jsonObj.getString("from");
 String toCurrency= jsonObj.getJSONObject("to");
 Double fromAmount = jsonObj.getDouble("from_amount");
 Double toAmount= jsonObj.getDouble("to_amount");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2014-10-02
    • 2021-07-01
    • 2018-01-27
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多