簡體   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