responseBody为{ "tag": { "id": 116, "name": "1685975583597" } }
时间: 2023-07-05 11:34:15 浏览: 49
根据你提供的 `Body`,我看到返回的是一个 JSON 对象,而不是一个字符串,所以不能直接使用正则表达式进行匹配。你需要先将其转换为 JavaScript 对象,然后再从对象中获取 `id` 属性。可以使用 `JSON.parse()` 方法将字符串转换为对象:
```
var responseJson = JSON.parse(responseBody);
var tag_id = responseJson.tag.id;
pm.globals.set("tag_id", tag_id);
console.log(tag_id);
```
这样应该就可以正确地获取 `tag` 对象中的 `id` 属性,并将其存储到 Postman 全局变量中。
相关问题
NumberFormatException: For input string: ","
NumberFormatException: For input string: ","是一个异常错误,表示字符串无法转换为数字。在提供的引用中,问题的根本原因是属性的注解信息出了问题,空字符串""无法转换为Number。解决这个问题的方法有两种:
1. 直接去掉引发异常的注解信息。可以将@ApiImplicitParams注解注释掉,代码如下:
```java
/**
* 信息
*/
@ResponseBody
@ApiOperation(value = "根据ID获取订单配置表信息")
// @ApiImplicitParams(@ApiImplicitParam(name = "id",value = "int类型",dataType = "int",required = true))
@RequestMapping(value = "/info/{id}",method = RequestMethod.GET)
public R info(@PathVariable("id") Integer id){
ApsOrderConfigEntity apsOrderConfig = apsOrderConfigService.getById(id);
return R.ok().put("apsOrderConfig", apsOrderConfig);
}
```
2. 在注解中添加一个example属性,并给出一个合法的数字值作为示例。示例中的合法数字值可以是任何整数,比如1。代码如下:
```java
/**
* 信息
*/
@ResponseBody
@ApiOperation(value = "根据ID获取订单配置表信息")
@ApiImplicitParams(@ApiImplicitParam(name = "id",value = "int类型",dataType = "int",required = true,example = "1"))
@RequestMapping(value = "/info/{id}",method = RequestMethod.GET)
public R info(@PathVariable("id") Integer id){
ApsOrderConfigEntity apsOrderConfig = apsOrderConfigService.getById(id);
return R.ok().put("apsOrderConfig", apsOrderConfig);
}
```
以上是两种解决NumberFormatException异常的方法。选择其中一种方法进行修改即可。
ResponseBody转换为json
@ResponseBody注解可以将方法的返回结果转换为JSON格式返回给前端。在Spring MVC中,当遇到@ResponseBody注解时,处理器会将该方法的响应类型标记为JSON数据集。在执行完控制器方法后,处理器会使用结果解析器(ResultResolver)去解析返回结果。解析器会轮询注册给Spring MVC的HttpMessageConverter接口的实现类,其中包括MappingJackson2HttpMessageConverter。如果找到匹配的转换器,它会将结果转换为JSON格式。如果找不到匹配的转换器,Spring MVC会继续处理后续流程。如果控制器的返回结果被MappingJackson2HttpMessageConverter转换,后续的模型和视图(ModelAndView)将返回null,视图解析器和视图渲染将不再执行。\[3\]
#### 引用[.reference_title]
- *1* [39、HttpMessageConverter的@ReponseBody处理json(转换为json字符串(浏览器才能识别))](https://blog.csdn.net/logtcm4/article/details/126912007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Java中@ResponseBody直接转换json](https://blog.csdn.net/weixin_42372731/article/details/114154320)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [【@ResponseBody注解如何将对象转换为json返回的】](https://blog.csdn.net/li646495946/article/details/124180924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)