[英]Parse JSON string in android
What I want to do is process live currency JSON strings and retrieve their rate values. 我想做的是处理实时货币JSON字符串并检索其汇率值。
The site I am using returns this: 我正在使用的网站返回此:
{"target": "EUR", "success": true, "rate": 0.7298, "source": "USD", "amount": 0.73, "message": ""}
for a URL: 网址:
http://currency-api.appspot.com/api/USD/EUR.json?key=KEY
For a conversion of USD to Euros. 将美元转换为欧元。 I want to store the rate in a float. 我想将汇率存储在浮动中。 I realise I will have to use GSON or something similar to parse the JSON output of the site, but so far I don't have a solution that works. 我意识到我将不得不使用GSON或类似的东西来解析站点的JSON输出,但是到目前为止,我还没有一个可行的解决方案。 My current AsyncTask for this looks like this: 我当前的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);;
}
}
What exactly is wrong here? 这到底是怎么了? It causes a runtime exception. 它导致运行时异常。 And how would I parse the rate out of this URL? 以及如何解析该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);;
}
}
To parse the JSON object, look at the docs 要解析JSON对象,请查看文档
For example 例如
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.