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;
    }

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐