解析JSON接口并批量插入到数据库中

接口为NBA接口json接口
使用hutool工具类在后端进行解析(防止跨域)

 @GetMapping("/pachou")
    public String teamMessage()  {

        s = HttpUtil.get("https://china.nba.cn/stats2/season/conferencestanding.json?");

        return s;
    }

获取前端传递的数据使用fastJSON进行解析并批量插入


    @PostMapping("/addBatch")
    public Result addBatch(@RequestBody String str) {


        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = JSONObject.parseObject(jsonArray.get(i).toString());
            String profile = json.getString("profile");
            JSONObject jsons = JSONObject.parseObject(profile);
            String city = jsons.getString("city");
            String abbr = jsons.getString("abbr");
            String cityEn = jsons.getString("cityEn");
            String displayConference = jsons.getString("displayConference");
            String displayAbbr = jsons.getString("displayAbbr");
            String nameEn = jsons.getString("nameEn");
            String division = jsons.getString("division");


            Team team = new Team();
            team.setAbbr( abbr);
            team.setCityEn(cityEn);
            team.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
            team.setDisplayConference(displayConference);
            team.setDisplayAbbr(displayAbbr);
            team.setNameEn(nameEn);
            team.setDivision(division);
            teamService.save(team);
        }


        return Result.success("成功");
    }

前端vue发送请求

 //发送数据
            send: function () {
                request.get("/team/pachou").then(res => {
                    console.log(res)
                    this.standingGroups = res.payload.standingGroups[1].teams
                    console.log(this.standingGroups)
                    // JSON.stringify(this.standingGroups)

                 /*   this.standingGroups.forEach(e=>{
                        JSON.stringify(this.standingGroups)
                        console.log(JSON.stringify(this.standingGroups));
                    })*/

                    request.post("/team/addBatch",this.standingGroups).then(res => {
                        console.log(res)
                    })

                })


            },

在这里插入图片描述
大功告成1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值