这里有一个数组,我们要做的先把它转成数组,然后是看他的数据里有没有“date”为12-17的日期,如果没有则新增一条。
[\n" +
"\n" +
"{\n" +
"\"date\":\"12-22\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
" {\n" +
"\"date\":\"12-17\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
" {\n" +
"\"date\":\"12-19\",\n" +
" \"A\": 1999,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
"{\n" +
"\"date\":\"12-16\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
"{\n" +
"\"date\":\"12-19\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" }\n" +
"]
首先我们先去pom里把这个加进去
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
输入json(记得要加双引号)
public static void main(String[] args) {
String a = "[\n" +
"\n" +
"{\n" +
"\"date\":\"12-22\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
" {\n" +
"\"date\":\"12-17\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
" {\n" +
"\"date\":\"12-19\",\n" +
" \"A\": 1999,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
"{\n" +
"\"date\":\"12-16\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" },\n" +
"{\n" +
"\"date\":\"12-19\",\n" +
" \"A\": 10,\n" +
" \"B\": 13.8,\n" +
" \"C\": 16.8\n" +
" }\n" +
"]";
然后把json转成数组
JSONArray array=JSON.parseArray(a);
按“date”大小进行排序(前面需要搞一个实体类Getter和Setter还有toString)
List<StreamTese> list = JSONObject.parseArray(array.toJSONString(),StreamTese.class);
List streamTese= list.stream().sorted(Comparator.comparing(StreamTese::getDate).reversed()).collect(toList());
System.out.println(streamTese);
获取当前时间(下面要做的只是同月不同日的比较,时间格式只取“MM-dd”就可以了)
//获取系统当前时间
Date date = new Date();
//时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
String now=simpleDateFormat.format(date);
继续循环判断
boolean b=false;
int i=0;
for ( i = 0; i <array.size() ; i++) {
//判断array数组中有没有和系统当前日期一样的数据“date”
//如果数组中没有等于系统当前时间的date,b为true。
if(!array.getJSONObject(i).getString("date").equals(now))
{
b=!b;
}
}
//如果b为false则新增
if (b){
Map<String,Object> map=new HashMap<>();
map.put("date","12-16");
map.put("A","10");
map.put("B","12.3");
map.put("C","10.6");
array.add(map);
System.out.println(streamTese);
}else {
//JSONObject jsonObject=array.getJSONObject(i-1);
//System.out.println(jsonObject.getString("date"));
//System.out.println(jsonObject.getString("A"));
//System.out.println(jsonObject.getString("B"));
//System.out.println(jsonObject.getString("C"));
System.out.println(streamTese);
}
}
}
打印结果:
[{"date":"12-22","A":10,"B":13.8,"C":16.8},{"date":"12-17","A":10,"B":13.8,"C":16.8},{"date":"12-16","A":1999,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8}]
[{"date":"12-22","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":1999,"B":13.8,"C":16.8},{"date":"12-17","A":10,"B":13.8,"C":16.8},{"date":"12-16","A":10,"B":13.8,"C":16.8}]
如果数组中没有“date”为12-16的数据,则新增
打印结果:
[{"date":"12-22","A":10,"B":13.8,"C":16.8},{"date":"12-17","A":10,"B":13.8,"C":16.8},{"date":"12-16","A":1999,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8}]
[{"date":"12-22","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":10,"B":13.8,"C":16.8},{"date":"12-19","A":1999,"B":13.8,"C":16.8},{"date":"12-17","A":10,"B":13.8,"C":16.8},{"date":"12-17","A":10,"B":13.8,"C":16.8},{"date":"12-16","A":"10","B":"12.3","C":"10.6"}]