java json string 转utf-8编码 工具类
时间: 2024-06-02 21:03:42 浏览: 17
在Java中,可以使用`java.nio.charset.StandardCharsets`类提供的`UTF_8`编码来将JSON字符串转换为UTF-8编码。下面是一个示例工具类的实现:
```java
import java.nio.charset.StandardCharsets;
public class JsonUtils {
public static String convertToJsonString(String jsonString) {
byte[] bytes = jsonString.getBytes(StandardCharsets.UTF_8);
return new String(bytes, StandardCharsets.UTF_8);
}
}
```
使用该工具类,你可以将JSON字符串转换为UTF-8编码的字符串。例如:
```java
String json = "{\"name\":\"John\",\"age\":30}";
String utf8Json = JsonUtils.convertToJsonString(json);
System.out.println(utf8Json);
```
输出结果将是UTF-8编码的JSON字符串。
相关问题
java httpclient post 请求
Java HttpClient是一个开源的HTTP客户端库,可以用来发送HTTP请求和接收HTTP响应。使用Java HttpClient发送POST请求,需要先创建一个HttpPost对象,然后设置请求头和请求体,最后执行请求并获取响应。
具体步骤如下:
1. 创建HttpPost对象
HttpPost post = new HttpPost(url);
其中,url是请求的地址。
2. 设置请求头
post.setHeader("Content-Type", "application/json");
其中,Content-Type是请求头中的一个参数,表示请求体的类型,这里设置为application/json。
3. 设置请求体
StringEntity entity = new StringEntity(json, "UTF-8");
post.setEntity(entity);
其中,json是请求体的内容,这里使用StringEntity将json转换为字符串,并设置编码为UTF-8。
4. 执行请求并获取响应
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);
其中,httpClient是HttpClient的实现类,HttpClients.createDefault()可以创建一个默认的httpClient实例,response是响应对象。
5. 处理响应
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
其中,EntityUtils是一个工具类,可以将响应体转换为字符串,这里设置编码为UTF-8。
以上就是使用Java HttpClient发送POST请求的步骤。
这个工具类不错,能把get请求也带body吗
GET请求本身是不支持带有请求体(RequestBody)的,因为GET请求的参数是通过URL传递的。但是,有些情况下,我们可能需要在GET请求中传递一些复杂的参数,比如JSON格式的参数,这时候可以把参数拼接到URL中,然后以普通的形式发送GET请求。下面是一个示例代码:
```java
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLEncoder;
public class HttpUtil {
/**
* 发送GET请求
*
* @param url 请求地址
* @return 响应结果
* @throws IOException
*/
public static String doGet(String url) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
}
} finally {
if (response != null) {
response.close();
}
httpClient.close();
}
return null;
}
/**
* 发送带有请求体的GET请求
*
* @param url 请求地址
* @param data 请求参数,JSON格式
* @return 响应结果
* @throws IOException
*/
public static String doGetWithBody(String url, String data) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url + "?" + URLEncoder.encode(data, "utf-8"));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
}
} finally {
if (response != null) {
response.close();
}
httpClient.close();
}
return null;
}
/**
* 发送POST请求
*
* @param url 请求地址
* @param data 请求参数,JSON格式
* @return 响应结果
* @throws IOException
*/
public static String doPost(String url, String data) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(data, "utf-8");
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json");
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
return EntityUtils.toString(responseEntity);
}
} finally {
if (response != null) {
response.close();
}
httpClient.close();
}
return null;
}
}
```
我们新增了一个名为doGetWithBody的方法,用于发送带有请求体的GET请求。在这个方法中,我们将请求体拼接到URL中,然后将URL作为参数传递给HttpGet实例。在使用这个方法时,我们只需要将请求体以JSON格式传递给这个方法即可。
需要注意的是,在将请求体拼接到URL中时,需要对请求体进行URL编码,否则可能会导致请求失败或响应结果不正确。