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

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱敲代码的小松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值