访问服务器json接口,将json字符串解析成json格式的demo

java 复制代码
 <!-- 其他依赖(如 JSON 解析) -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.83</version>
    </dependency>
java 复制代码
package com.tools;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
public class ApiClient {
//将json字符串解析成json格式
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//{"Name":"CICI","Phone":"18501680095","Img":"http://pwxvr.com/dreame/statics/uploadfiles/1773206309811.jpg","BU":"","code":200,"msg":"查询用户信息成功"}

		String jsonData = getUserData("D00001");
        System.out.println(jsonData);
		
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        String code = jsonObject.getString("code");
        System.out.println("code: " + code); // 输出:code: 200(查询成功) 、其他的数字,代表查询失败
        
        String name = jsonObject.getString("Name");
        System.out.println("Name: " + name); // 输出:Name: CICI
        String bu = jsonObject.getString("BU");
        System.out.println("BU: " + bu); // 输出:BU: 用户的BU名称
        String img = jsonObject.getString("Img");
        System.out.println("Img: " + img); // 输出Img: 头像路径
        
	}

	
	public static String getUserData(String cardId) {
        HttpURLConnection connection = null;
        try {
            // 构建URL
            String urlStr = "http://pwx.com/dreame/ac/selectPlaceuser.json?cardId=" + cardId;
            URL url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            
            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            
            // 获取响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应内容
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "UTF-8")
                );
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                return response.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
	
}
相关推荐
一叶飘零_sweeeet21 分钟前
Docker Compose实战指南
运维·docker·容器
Bruce_Liuxiaowei37 分钟前
Windows防火墙规则导出工具:让安全配置可备份、可迁移、可审计
运维·windows·安全·网络安全
SPC的存折1 小时前
10、Docker容器故障排查
linux·运维·数据库·docker·容器
liuyunshengsir2 小时前
linux 下新增用户后无法使用TAB补全功能的最佳解决方法
linux·运维·服务器
志栋智能3 小时前
超自动化巡检:驱动运维团队从操作走向优化
运维·服务器·自动化
乌托邦的逃亡者3 小时前
Dockerfile的配置和使用
linux·运维·docker·容器
小此方3 小时前
Re:Linux系统篇(三)指令篇 · 二:十二个高频指令精讲+重定向操作+“一切皆文件“深入理解
linux·运维·服务器
用户1401056775194 小时前
线上接口偶发超时,最后发现是 conntrack 打满:一次网络故障排查实战
运维
以太浮标4 小时前
华为eNSP模拟器综合实验之- 主机没有配置缺省网关时,通过路由式Proxy ARP实现通信(arp-proxy enable)
运维·网络·网络协议·华为·智能路由器·信息与通信
赵庆明老师5 小时前
vben开发入门6:tsconfig.json
json·vue3·vben