我有以下JSON数据,这是一个JSON数组:
{"Temperature":"26.10","Date":"2016-04-11 14:45:25"}
我已尝试使用以下代码进行解析,但我不确定我是否正确执行此操作:
JSONArray jArr = new JSONArray (data);
JSONArray tempArr = getArray("Temperature", jArr);
String temp = jArr.getString("Temperature");
TextView Temp1 = (TextView) findViewById(R.id.textTemp);
Temp1.setText("Temperature: " + temp);
编辑:所以我搞砸了,弄错了,它应该是一个JSON对象
答案 0 :(得分:0)
绝对错误..亲爱的只看到你的json数据..是一个JSONObject不是一个json数组。正确的方法如下所示
JSONObject json = new JSONObject (data);
String temp = json.optString("Temperature");
String date = json.optString("Date");
TextView Temp1 = (TextView) findViewById(R.id.textTemp);
Temp1.setText("Temperature: " + temp);
答案 1 :(得分:0)
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(data);
String temp = jsonObject.getString("Temperature");
TextView Temp1 = (TextView) findViewById(R.id.textTemp);
Temp1.setText("Temperature: " + temp);
} catch (JSONException e) {
e.printStackTrace();
}
实际上你的数据不是JSONArray格式,它是JSONObject格式,所以你需要用JSONObject解析
答案 2 :(得分:0)
因为有两个名称不同的对象。因此,您必须使用不同的textview访问温度和日期信息。它将如下所示: -
JSONObject json = new JSONObject (data);
String temp = json.optString("Temperature");
String date = json.optString("Date");
TextView Temp1 = (TextView) findViewById(R.id.textTemp);
Temp1.setText("Temperature: " + temp);
TextView Date1 = (TextView) findViewById(R.id.textDate);
Date1.setText("Date: " + date);