JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。在前后台的传输过程中,Json字符串是相当常用的,这里就不多介绍其功能了,直接举一下应用的小例子,帮助理解这两个方法的用法。
//LIST列表
//根据gameuuid查找对应的item子项列表
@RequestMapping(value = "/findGameitemlistBygameUUId", method = RequestMethod.POST,produces="application/json; utf-8")
public List<Gameitemlist> findGameitemlistBygameUUId(String gameuuid) {
String redisValue = stringRedisTemplate.opsForValue().get("findGameitemlistBygameUUId" + gameuuid);
List<Gameitemlist> gameitemlists = new ArrayList<>();
if(redisValue == null ||redisValue.equals(""))
{
System.out.println("读取db");
gameitemlists = gameitemlistRepository.findGameItemsByGameUUID(gameuuid);
stringRedisTemplate.opsForValue().set("findGameitemlistBygameUUId" + gameuuid, JSON.toJSONString(gameitemlists),overtime,TimeUnit.HOURS);
}else{
System.out.println("读取缓存");
gameitemlists = JSON.parseArray(redisValue,Gameitemlist.class);
}
return gameitemlists;
}
//单个对象
//根据playeruuid 从PlayerInfo里面获得报名者详细数据
@RequestMapping(value = "/findPlayerinfoByPlayerUUID", method = RequestMethod.POST,produces="application/json; utf-8")
public Playerinfo findPlayerinfoByPlayerUUID(String playeruuid) {
String redisValue = stringRedisTemplate.opsForValue().get("findPlayerinfoByPlayerUUID" + playeruuid);
Playerinfo playerinfo = new Playerinfo();
if(redisValue == null ||redisValue.equals("")){
System.out.println("读取db");
playerinfo = (Playerinfo) playinfoRepository.findPlayerinfoByPlayerUUID(playeruuid);
stringRedisTemplate.opsForValue().set("findPlayerinfoByPlayerUUID" + playeruuid, JSON.toJSONString(playerinfo),overtime,TimeUnit.HOURS);
}else{
System.out.println("读取缓存");
playerinfo = (Playerinfo) JSON.parseObject(redisValue,Playerinfo.class);
}
return playerinfo;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/106018.html