java获取json格式数据后的处理方式

public void saveDataAsset() {
HttpUtil util = new HttpUtil();
String response = util.httpUrl(“http://XXXXXXXXXX”);
Map result = JSON.parseObject(response);//获取接送对象转换为Map类型
Map map = (Map) result.get(“map”);
List taskCountList = JSONObject.parseArray(((JSONArray) map.get(“taskCountList”)).toJSONString(), TaskFinishVo.class);
/(JSONArray) map.get(“taskCountList”) 获取 key为taskCountList的数据并转换为JsonArray,然后通过parseArray方法将其转换成相关list对象/
}

//获取数据的方法
public String httpUrl(String urlStr){
String response = “”;
try {
//请求的webservice的url
URL url = new URL(urlStr);
//创建http链接
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//设置请求的方法类型
httpURLConnection.setRequestMethod(“POST”);
//设置请求的内容类型
httpURLConnection.setRequestProperty(“Content-type”, “application/x-www-form-urlencoded”);
//设置发送数据
httpURLConnection.setDoOutput(true);
//设置接受数据
httpURLConnection.setDoInput(true);
//发送数据,使用输出流
OutputStream outputStream = httpURLConnection.getOutputStream();
//发送的soap协议的数据
//String requestXmlString = requestXml(“北京”);
String content = “user_id=”+ URLEncoder.encode(“13846”, “gbk”);
//发送数据
outputStream.write(content.getBytes());
//接收数据
InputStream inputStream = httpURLConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
InputStreamReader isr = new InputStreamReader(inputStream,“utf-8”);
BufferedReader br = new BufferedReader(isr);
String str = null;
while((str = br.readLine())!=null){
buffer.append(str);
}
br.close();
isr.close();
inputStream.close();
response = buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println(“模拟调用数据监控接口:发生变化的指标:”+key+",指标值:"+value+"。受影响的数据-----:"+response);
return response;
}

参考文档:https://blog.csdn.net/qq_36004521/article/details/80101881

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值