json转List对象,com.alibaba.fastjson.JSONException: syntax error, pos 1, json : http:/

报错:com.alibaba.fastjson.JSONException: syntax error, pos 1, json : http:/

原因是因为接口返回的有空值,最简单的办法就是创建一个实体类接收

String json = HttpUtils.get("http://ip/blank",null);
List<Blankstock> list = JSONObject.parseArray(json,Blankstock.class);

 public static String get(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            String getUrl = url+"?";
            if (paramsMap != null) {
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), ENCODING)+"&";
                }
            }
            HttpGet method = new HttpGet(getUrl);
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            log.error("http request failed",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                log.error("",e);
            }
        }
        return responseText;
    }

 

//获取了单个的对象,可以对这个进行操作
for(Blankstock Blankstock:list){
    //插入数据库的操作
    this.blankstockMapper.insert(Blankstock);
}

 

获取http接口数据,然后转化为类list

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值