java后端通过url接口获取甲方数据库并转化为json

项目在对接甲方数据库的时候,需要甲方提供接口来给我方传输数据,双方规定好以json的格式传送,为此整了一个通过url获取数据并转化为json的util

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetJson {
    public JSONObject getHttpJson(String url, int comefrom) throws Exception {
        try {
            System.out.println("url:"+url);
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            System.out.println("code:"+connection.getResponseCode());
            System.out.println("getErrorStream:"+connection.getErrorStream());
            //请求成功
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream is = connection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                //10MB的缓存
                byte[] buffer = new byte[10485760]; //is.available()
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                String jsonString = baos.toString();
                baos.close();
                is.close();
                //转换成json数据处理
                // getHttpJson函数的后面的参数1,表示返回的是json数据,2表示http接口的数据在一个()中的数据
                JSONObject jsonArray = getJsonString(jsonString, comefrom);
                return jsonArray;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getJsonString(String str, int comefrom) throws Exception{
        JSONObject jo = null;
        if(comefrom==1){
            return new JSONObject(str);
        }else if(comefrom==2){
            int indexStart = 0;
            //字符处理
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)=='('){
                    indexStart = i;
                    break;
                }
            }
            String strNew = "";
            //分割字符串
            for(int i=indexStart+1;i<str.length()-1;i++){
                strNew += str.charAt(i);
            }
            return new JSONObject(strNew);
        }
        return jo;
    }
}

获取之后用fastjson等等json处理工具来处理都可以,下面采用的是阿里巴巴的fastjson,如:

@Test
    public void findXXX(){
        String address="http://localhost:8080/well/xxxxx";
        try {
            JSONObject dayLine = new com.example.liuz.json.GetJson().getHttpJson(address,1);
            //取得data的json数据
            //JSONArray json=dayLine.getJSONArray("result");
            JSONObject json = dayLine.getJSONObject("result");
            System.out.println(json.toString());
            System.out.println("==========================================");
            JSONArray array = json.getJSONArray("dBItemList");
            System.out.println(array.toString());
            System.out.println("==========================================");
            //JSONObject info=array.getJSONObject(0);
            List<BizDesignWellSectionTime> list= JSON.parseArray(array.toString(), BizDesignWellSectionTime.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

需要注意到,什么时候使用JSONArray,什么时候使用JSONObject。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值