需求
现在返回json数据带有分页的数据,将返回data属性数据变更为content,数据不变,key发生变化
实现1,源数据比较复杂,组装数据比较麻烦
说明:如果使用这种方式完成需求,需要创建对象,一个和PageInfo一样的对象,list属性修改为content,其他拷贝过去就行了。
但是,如果数据量10w左右,还是按照这种方式,json转换比较耗时,这种方式虽然麻烦,效率快一点。
@Test
public void t2(){
log.info("需求:k1变为k2,value不变");
Map<String, Object> map = new HashMap<>();
map.put("k1","v1");
log.info("修改前");
for (Map.Entry<String, Object> entry : map.entrySet()) {
log.info("key:【{}】,value:【{}】",entry.getKey(),entry.getValue());
}
String pendingItem = map.get("k1").toString();
map.remove("k1");
map.put("k2",pendingItem);
log.info("修改后");
for (Map.Entry<String, Object> entry : map.entrySet()) {
log.info("key:【{}】,value:【{}】",entry.getKey(),entry.getValue());
}
}
Connected to the target VM, address: ‘127.0.0.1:5463’, transport: ‘socket’
20:12:41.966 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - 需求:k1变为k2,value不变
20:12:41.984 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - 修改前
20:12:41.984 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - key:【k1】,value:【v1】
20:12:41.989 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - 修改后
20:12:41.990 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - key:【k2】,value:【v1】
Disconnected from the target VM, address: ‘127.0.0.1:5463’, transport: ‘socket’
文章来源:https://www.toymoban.com/news/detail-630645.html
实现2,通过工具类json转换,注意数据量大时候不建议使用
String listSTR = "{\n" +
"\t\t\"startRow\": 3,\n" +
"\t\t\"navigatepageNums\": [1, 2],\n" +
"\t\t\"prePage\": 1,\n" +
"\t\t\"hasNextPage\": false,\n" +
"\t\t\"nextPage\": 0,\n" +
"\t\t\"pageSize\": 2,\n" +
"\t\t\"endRow\": 3,\n" +
"\t\t\"list\": [{\n" +
"\t\t\t\"teacherName\": \"小2\",\n" +
"\t\t\t\"teacherNo\": \"6\",\n" +
"\t\t\t\"categoryId\": 9999\n" +
"\t\t}],\n" +
"\t\t\"pageNum\": 2,\n" +
"\t\t\"navigatePages\": 8,\n" +
"\t\t\"total\": 3,\n" +
"\t\t\"navigateFirstPage\": 1,\n" +
"\t\t\"pages\": 2,\n" +
"\t\t\"size\": 1,\n" +
"\t\t\"isLastPage\": true,\n" +
"\t\t\"hasPreviousPage\": true,\n" +
"\t\t\"navigateLastPage\": 2,\n" +
"\t\t\"isFirstPage\": false\n" +
"\t}";
JSONObject jsonObject1 = JSONObject.parseObject(listSTR);
Object newList = jsonObject1.get("list");
jsonObject1.remove("list");
jsonObject1.put("content", newList);
list属性转换为data属性
注:如果有更好的方式,希望大家多多指教。文章来源地址https://www.toymoban.com/news/detail-630645.html
到了这里,关于解决Map修改key的问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!