HttpUtil string转JSONObject
时间: 2023-08-23 11:15:51 浏览: 46
HttpUtil类中的doPost方法返回的是一个ResultVO对象,通过ResultVO对象可以获取data数据。首先需要将data数据转换为字符串,可以使用resultVO.getData().toString()方法。然后,使用JSONObject.parseObject方法将字符串转换为JSONObject对象。例如,可以按照以下方式进行转换:
```
ResultVO resultVO = HttpUtil.doPost(appConfig.getZXurl(), s1, null);
JSONObject jsonObject = JSONObject.parseObject(resultVO.getData().toString());
```
这样就可以将HttpUtil中返回的字符串转换为JSONObject对象了。引用自。
相关问题
http post请求 jsonObject 工具类
以下是一个Java中发送HTTP POST请求并传递JSON对象的工具类:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class HttpUtil {
public static String sendPost(String url, JSONObject json) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为POST
con.setRequestMethod("POST");
// 设置请求头部信息
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
// 允许输出流,即允许向服务器发送数据
con.setDoOutput(true);
// 创建JSON对象
JSONObject jsonObject = json;
// 将JSON对象转换成字符串
String jsonBody = jsonObject.toString();
// 获取输出流对象
OutputStream os = con.getOutputStream();
// 向输出流写入JSON字符串
os.write(jsonBody.getBytes());
// 刷新输出流,确保数据被发送到服务器
os.flush();
// 关闭输出流
os.close();
// 获取响应码
int responseCode = con.getResponseCode();
// 读取响应内容
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// 关闭输入流
in.close();
// 返回响应结果
return response.toString();
}
}
```
使用时,只需调用`sendPost`方法即可:
```java
JSONObject json = new JSONObject();
json.put("key1", "value1");
json.put("key2", "value2");
String url = "http://example.com/api";
String response = HttpUtil.sendPost(url, json);
```
其中,`json`为要传递的JSON对象,`url`为要请求的URL地址,`response`为服务器返回的响应结果。
分析以下代码: public static Dict getIPAndCity() { Dict dict = Dict.create(); try { Document document = Jsoup.connect("https://ip.900cha.com/").get(); // /html/body/div/div/div/div[1]/div[1]/h3 String ip = document.selectXpath("/html/body/div/div/div/div[1]/div[1]/h3").get(0).text().trim(); // 通过 Xpath 直接获取了网页里面的内容 dict.set("ip", ip); String city = ""; if (IP_MAP.get(ip) != null) { city = IP_MAP.get(ip); dict.set("city", city); return dict; } String url = "https://api.map.baidu.com/location/ip?ip=" + ip + "&ak=bmvg8yeOopwOB4aHl5uvx52rgIa3VrPO"; String res = HttpUtil.createRequest(Method.GET, url).execute().body(); String json = UnicodeUtil.toString(res); JSON jsonObject = JSONUtil.parseObj(json); city = jsonObject.getByPath("content.address", String.class); dict.set("city", city); } catch (Exception e) { log.error("获取IP失败", e); } return dict; } }
这段代码的作用是获取当前机器的公网IP地址以及所在城市信息,并将结果封装到一个Dict对象中返回。
具体实现过程如下:
1. 创建一个空的Dict对象。
2. 通过Jsoup框架向一个网站(https://ip.900cha.com/)发送请求,并获取返回的HTML文档。
3. 使用xpath语法获取HTML文档中指定元素的文本内容,即当前机器的公网IP地址,并将其存入Dict对象中。
4. 判断当前IP地址是否已经被存储在一个名为IP_MAP的Map对象中,如果已经存在,则直接从Map对象中获取对应的城市信息并存入Dict对象中。
5. 如果当前IP地址不存在于IP_MAP中,则向百度地图API发送请求,通过IP地址获取所在城市的信息,并将城市信息存入Dict对象中。
6. 如果在任何步骤中出现了异常,则记录日志,并返回一个空的Dict对象。
7. 最后,将存储有IP地址和城市信息的Dict对象返回给调用者。