json.tostring
时间: 2023-08-14 15:13:01 浏览: 55
JSON.stringify() 方法是将 JavaScript 对象或值转换为 JSON 字符串的方法。它接收一个参数,即要转换的对象或值,并返回一个表示该对象或值的 JSON 字符串。
示例用法:
```
const obj = { name: 'John', age: 30 };
const jsonString = JSON.stringify(obj);
console.log(jsonString);
// 输出:{"name":"John","age":30}
```
注意:JSON.stringify() 方法不能处理包含循环引用的对象,否则会抛出异常。
相关问题
List<WEMC0010> targets = dao.query(queryFunc, map); JSON json = JSONSerializer.toJSON(targets); inInfo.set(blockName, json.toString());什么意思
这段代码的作用是从数据库中查询符合条件的数据,并将结果以 JSON 格式的字符串形式存储到变量 `inInfo` 的属性中。
具体来说,`dao.query(queryFunc, map)` 是调用了 `dao` 对象的 `query` 方法,该方法接受两个参数:一个是查询函数 `queryFunc`,另一个是查询参数 `map`。它返回符合条件的结果集合,类型为 `List<WEMC0010>`。
接着,`JSONSerializer.toJSON(targets)` 将结果集合 `targets` 转换为 JSON 对象 `json`。
最后,`inInfo.set(blockName, json.toString())` 将 JSON 对象 `json` 转换为字符串,并将其存储到 `inInfo` 对象的属性中,属性名为 `blockName`。这样,就可以将查询结果以 JSON 字符串的形式保存到变量 `inInfo` 中,以便后续处理。
优化这段代码@PostMapping("/login") @ResponseBody @PassToken public Object login(String username,String password) throws IOException { String url="http://42.177.95.222:9202/platform/yugang/task/getToken"; HttpClient client = HttpClients.createDefault(); //默认post请求 HttpPost post = new HttpPost(url); //拼接多参数 JSONObject json = new JSONObject(); json.put("username", "渔业协会"); json.put("password", "Yuye!@#qwe"); post.addHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Accept", "application/json"); post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8"))); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); String s = EntityUtils.toString(entity); JSONObject jo = JSONObject.parseObject(s); String token = jo.getJSONObject("data").getString("token"); System.out.println("dsadasdasdsadasd"+token); return verify(token); } public Object verify(String token) throws IOException { String url="http://42.177.95.222:9202/platform/yugang/task/verify"; HttpClient client = HttpClients.createDefault(); //默认post请求 HttpPost post = new HttpPost(url); //拼接多参数 JSONObject json = new JSONObject(); json.put("token",token); post.addHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Accept", "application/json"); post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8"))); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); String s = EntityUtils.toString(entity); JSONObject jo = JSONObject.parseObject(s); return jo; }
这段代码的优化点有以下几个:
1. 将重复的代码抽取成公共方法,避免代码冗余。
2. 使用连接池管理 HttpClient 的连接,提高连接的复用率和性能。
3. 使用 try-with-resources 语句关闭 HttpClient 和 HttpEntity 对象,避免资源泄漏。
下面是代码优化后的示例:
```
private static final String BASE_URL = "http://42.177.95.222:9202/platform/yugang/task";
private CloseableHttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(200)
.setMaxConnPerRoute(100)
.build();
@PostMapping("/login")
@ResponseBody
@PassToken
public Object login(String username, String password) throws IOException {
String token = getToken();
System.out.println("dsadasdasdsadasd" + token);
return verify(token);
}
private String getToken() throws IOException {
String url = BASE_URL + "/getToken";
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
JSONObject json = new JSONObject();
json.put("username", "渔业协会");
json.put("password", "Yuye!@#qwe");
post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8")));
try (CloseableHttpResponse response = httpClient.execute(post)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
JSONObject jo = JSONObject.parseObject(s);
return jo.getJSONObject("data").getString("token");
}
return null;
}
}
private JSONObject verify(String token) throws IOException {
String url = BASE_URL + "/verify";
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
JSONObject json = new JSONObject();
json.put("token", token);
post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8")));
try (CloseableHttpResponse response = httpClient.execute(post)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
return JSONObject.parseObject(s);
}
return null;
}
}
```
在优化后的代码中,我们将 HttpClient 对象的创建和关闭都放在了方法外部,使用连接池管理 HttpClient 的连接,提高连接的复用率和性能。同时,我们也将 getToken 和 verify 方法抽取出来,避免了代码冗余。最后,我们使用了 try-with-resources 语句来关闭 HttpClient 和 HttpEntity 对象,避免了资源泄漏。