如何用这种格式读取json

时间:2014-06-13 13:24:04

标签: java android json

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

api url

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

我试着做下面的片段。但它没有返回任何东西,也没有在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));

                                } 

1 个答案:

答案 0 :(得分:0)

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

 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");