解析android中的JSON字符串

时间:2013-12-08 18:01:38

标签: java android json

我想要做的是处理实时货币JSON字符串并检索它们的费率值。

我正在使用的网站返回:

{"target": "EUR", "success": true, "rate": 0.7298, "source": "USD", "amount": 0.73, "message": ""}

表示网址:

http://currency-api.appspot.com/api/USD/EUR.json?key=KEY

将美元兑换成欧元。我想将汇率存入浮点数。我意识到我将不得不使用GSON或类似的东西来解析网站的JSON输出,但到目前为止我没有一个有效的解决方案。我目前的AsyncTask看起来像这样:

        class AsyncTest extends AsyncTask<Void,Void,Void>
    {
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params)
        {
            try {
                URL url = new URL("http://currency-api.appspot.com/api/USD/EUR.json?key=KEY");
                URLConnection connect = url.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
                String jsonObject = "";
                String line = "";
                while ((line = in.readLine()) != null)
                    jsonObject += line;
                in.close();
                Toast.makeText(getApplicationContext(), jsonObject, Toast.LENGTH_LONG).show();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);;
        }

    }

这到底出了什么问题?它会导致运行时异常。我将如何解析此网址的费率?

2 个答案:

答案 0 :(得分:0)

    class AsyncTest extends AsyncTask<Void,Void,Void>
{
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        try {
            URL url = new URL("http://currency-api.appspot.com/api/USD/EUR.json?key=KEY");
            URLConnection connect = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            StringBuilder jsonObject = new StringBuilder();
            String line = "";
            while ((line = in.readLine()) != null)
                jsonObject.append( line );
            in.close();
            Toast.makeText(getApplicationContext(), jsonObject.toString(), Toast.LENGTH_LONG).show();
            JSONObject json = new JSONObject(jsonObject.toString());
            ///// Parse the Json object right here using getString

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);;
    }

}

要解析JSON对象,请查看docs

例如

private void parseJson(JSONObject json){
    String target = json.getString("target");
    boolean success = json.getBoolean("success");
    // If the field is optional, use optXXX
    double rate = json.optDouble("rate", 1d);
    ......

}

答案 1 :(得分:0)

尝试捕获JSONException。