我尝试解析响应,并尝试从响应中获取Date对象,但无法获取它。谁能告诉我如何获取Date对象。
{
"flag":"success",
"msg":[
{
"2018-10-01":{
"date":"2018-10-01",
"login_time":"1538393123",
"logout_time":"",
"logout_message":"",
"lock_time":"1538393236,1538393671,1538393764",
"unlock_message":"testing,testing,break time",
"unlock_time":"1538393363,1538393680,1538395633"
}
},
{
"2018-10-03":{
"date":"2018-10-03",
"login_time":"1538548533",
"logout_time":"",
"logout_message":"",
"lock_time":"1538560561,1538561016,1538561260,1538561881",
"unlock_message":"hey,gggg,gggg5555,fd",
"unlock_time":"1538560617,1538561100,1538561273,1538566017"
}
}
]
}
答案 0 :(得分:1)
尝试一下
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("msg");
for(int i=0;i < jsonArray.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
Iterator<?> keys = obj.keys();
while( keys.hasNext() ) {
String key = (String)keys.next();
if(obj.get(key) instanceof JSONObject) {
JSONObject dateObj = (JSONObject) obj.get(key);
String DATE = dateObj.getString("date");
Log.d("DATE",DATE);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
这是一个小的演示代码。如果您经常使用SimpleDateFormat
,请重新使用它。
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(object.optJSONArray("msg").optJSONObject(i).optString("date","").getTime();
答案 2 :(得分:0)
var object = {"flag":"success","msg":[{"2018-10-01":{"date":"2018-10-01","login_time":"1538393123","logout_time":"","logout_message":"","lock_time":"1538393236,1538393671,1538393764","unlock_message":"testing,testing,break time","unlock_time":"1538393363,1538393680,1538395633"}},{"2018-10-03":{"date":"2018-10-03","login_time":"1538548533","logout_time":"","logout_message":"","lock_time":"1538560561,1538561016,1538561260,1538561881","unlock_message":"hey,gggg,gggg5555,fd","unlock_time":"1538560617,1538561100,1538561273,1538566017"}}]};
var msg = object.msg;
var dateobjects = []; // to store the date objects
//to go through the msg array items
for(var i = 0; i < msg.length; i++)
{
let current = msg[i];
for(var prop in current)
dateobjects.push(current[prop]);
}
// Print date objects in console
console.log(dateobjects);