JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。在前后台的传输过程中,Json字符串是相当常用的,这里就不多介绍其功能了,直接举一下应用的小例子,帮助理解这两个方法的用法。文章来源地址https://www.toymoban.com/news/detail-533751.html
//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.toymoban.com/news/detail-533751.html
到了这里,关于json中的json.parseObject()方法和json.tojsonString()方法讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!