json中的json.parseObject()方法和json.tojsonString()方法讲解

JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。在前后台的传输过程中,Json字符串是相当常用的,这里就不多介绍其功能了,直接举一下应用的小例子,帮助理解这两个方法的用法。

  //LIST列表
  //根据gameuuid查找对应的item子项列表
    @RequestMapping(value = "/findGameitemlistBygameUUId", method = RequestMethod.POST,produces="application/json; utf-8")
    public List findGameitemlistBygameUUId(String gameuuid) {

        String redisValue = stringRedisTemplate.opsForValue().get("findGameitemlistBygameUUId" + gameuuid);
        List 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;
    }

你可能感兴趣的:(Spring,boot,json,java,postman)