这是我从服务器解析时的json:
{
"main": [
{
"id": "12345"
}
],
"parameter": [
{
"temp": "30.00",
"acc": "3.00",
"moisture": "200.00",
"battery": "98.00",
"date": "2015-04-13",
"time": "14:51:05"
},
{
"temp": "32.00",
"acc": "2.50",
"moisture": "190.00",
"battery": "80.00",
"date": "2015-04-13",
"time": "14:53:21"
},
{
"temp": "27.00",
"acc": "5.00",
"moisture": "200.00",
"battery": "60.00",
"date": "2015-04-13",
"time": "15:06:04"
},
{
"temp": "21.00",
"acc": "3.00",
"moisture": "160.00",
"battery": "60.00",
"date": "2015-04-13",
"time": "15:07:13"
},
{
"temp": "30.00",
"acc": "4.50",
"moisture": "200.00",
"battery": "65.00",
"date": "2015-04-13",
"time": "10:18:11"
}
]
}
我有一种方法将时间从格式“hh:mm:ss”转换为“hh”。
public static int convertTime(String time) {
Date formatTime = null;
try {
formatTime = new SimpleDateFormat("hh:mm:ss").parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@SuppressWarnings("deprecation")
int hour = formatTime.getHours();
return hour;
}
然后我要计算每次的平均温度。例如,当我转换为“hh”时,time=“14”有两个温度值,分别是“30和32”。如何计算平均温度?拜托,帮帮我。
3条答案
按热度按时间rm5edbpk1#
最后,你将得到数组中所有特定时间的平均温度
kx5bkwkv2#
nszi6y053#
@vishwaijt palankar,这是我的代码,请检查,当我运行logcat时,有更多的totaltemp和occurrence值。