[
{
"id":"1",
"created_at":"2019-08-19 02:54:36",
"updated_at":"2019-09-04 15:00:05"
},
{
"id":"2",
"created_at":"2019-08-27 08:59:18",
"updated_at":"2019-09-04 14:59:14"
},
{
"id":"4",
"created_at":"2019-08-29 20:19:54",
"updated_at":"2019-09-04 14:58:53"
}
]
如何根据降序对“ created_at”(2019-08-30,2019-08-29)数据排序json数据并将值设置为android中的textview。
答案 0 :(得分:1)
请尝试
public static JSONArray sortJsonArray(JSONArray array) {
List<JSONObject> jsons = new ArrayList<JSONObject>();
for (int i = 0; i < array.length(); i++) {
jsons.add(array.getJSONObject(i));
}
Collections.sort(jsons, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject lhs, JSONObject rhs) {
String lid = lhs.getString("created_at");
String rid = rhs.getString("created_at");
// Here you could parse string id to integer and then compare.
return lid.compareTo(rid);
}
});
return new JSONArray(jsons);
}
答案 1 :(得分:0)
使用gson库的解决方案:
public static void setSortedDate (String json, TextView tv) {
Type t = new TypeToken<List<DateModel>>(){}.getType();
Gson gson = new Gson();
List<DateModel> list = gson.fromJson(json, t);
Collections.sort(list, new Comparator<DateModel>(){
@Override
public int compare (DateModel p1, DateModel p2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date d1 = df.parse(p1.created_at);
Date d2 = df.parse(p2.created_at);
return d2.compareTo(d1);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
});
int i = list.size();
for(DateModel d: list){
tv.append(d.created_at);
if(--i > 0) tv.append(", ");
}
}
模型类
public class DateModel {
String id;
String created_at;
String updated_at;
}
用法
private static final String JSON = "[ { \"id\": \"1\", \"created_at\": \"2019-08-19 02:54:36\", \"updated_at\": \"2019-09-04 15:00:05\" }, { \"id\": \"2\", \"created_at\": \"2019-08-27 08:59:18\", \"updated_at\": \"2019-09-04 14:59:14\" }, { \"id\": \"4\", \"created_at\": \"2019-08-29 20:19:54\", \"updated_at\": \"2019-09-04 14:58:53\" }, { \"id\": \"5\", \"created_at\": \"2019-08-30 09:31:42\", \"updated_at\": \"2019-09-04 14:58:40\" } ]";
setSortedDate(JSON, tv);
输出
2019-08-30 09:31:42,2019-08-29 20:19:54,2019-08-27 08:59:18,2019-08-19 02:54:36
更新
这是用标准的Java实现代替gson
public static void setSortedDate (String json, TextView tv) {
List<DateModel> list = getListFromJson(json);
Collections.sort(list, new DateModelComparator());
int i = list.size();
for(DateModel d: list){
tv.append(d.created_at);
if(--i > 0) tv.append(", ");
}
}
private static List<DateModel> getListFromJson (String json) {
List<DateModel> list = new LinkedList<>();
try {
JSONArray array = new JSONArray(json);
for(int i=0;i<array.length();i++){
JSONObject obj = array.getJSONObject(i);
DateModel dm = new DateModel();
dm.id = obj.getString("id");
dm.created_at = obj.getString("created_at");
dm.updated_at = obj.getString("updated_at");
list.add(dm);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
DateComparator 类
public class DateModelComparator implements Comparator<DateModel> {
@Override
public int compare (DateModel p1, DateModel p2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
Date d1 = df.parse(p1.created_at);
Date d2 = df.parse(p2.created_at);
return d2.compareTo(d1);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
}