(Java)从URL中读取Json数据中某一个key的value

本文介绍如何在Java中从HTTPS URL读取JSON数据,并着重讲解如何跳过SSL证书检测,以获取特定key的value。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static String loadJson (String url) {

        String username = "*****";
        String password = "******";
        StringBuilder json = new StringBuilder();


        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();

            String encoding = new String(Base64.encode(new                     
            String(username+":"+password).getBytes()));
                ((HttpURLConnection) uc).setRequestMethod("GET");
                uc.setRequestProperty("Content-Type", "text/plain");
                uc.setRequestProperty("charset", "UTF-8");
                uc.setRequestProperty( "Authorization","Basic "+encoding);
                uc.connect();

            BufferedReader in = new BufferedReader(new 
            InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }


    //跳过SSL证书检测
    private static void disableSslVerification() {
        try
        {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) 
                {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) 
                {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }
public static void main(String[] args) {
        disableSslVerification();
        String url1 = "https://10.0.19.195/redfish/v1/Chassis/1/NetworkAdapters/PCIeSlot1/NetworkPorts/1";
        String json = loadJson(url1);
        JSONObject jsonObject = JSONObject.fromObject(json);
        System.out.println(jsonObject.get("AssociatedNetworkAddresses"));
        
    }

注:如果url是https开头的,跳过SSL证书检测

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值