繁体   English   中英

在Android中解析JSON字符串

[英]Parse JSON string in android

我想做的是处理实时货币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);;
        }

    }

这到底是怎么了? 它导致运行时异常。 以及如何解析该URL的汇率?

    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对象,请查看文档

例如

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

}

尝试捕获JSONException。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM