fastjson对象转map
时间: 2023-09-09 19:07:01 浏览: 113
FastJson提供了将Java对象转换为Map的方法。可以使用FastJson的`toJSONString`方法将对象转换为JSON字符串,然后使用`parseObject`方法将JSON字符串转换为Map。具体代码如下:
```
User user = new User();
user.setName("校长");
user.setAge(3);
user.setSalary(new BigDecimal("123456789.0123"));
// 对象转换为Map
String jsonString = JSON.toJSONString(user); // 将对象转换为JSON字符串
Map<String, Object> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Object>>(){});
System.out.println("map = " + map); // 输出转换后的Map
```
以上代码将对象`user`转换为JSON字符串`jsonString`,然后使用`parseObject`方法将JSON字符串转换为Map对象`map`。最后打印输出转换后的Map。
注意:需要导入FastJson的相关依赖,并引入FastJson的包进行转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [FastJson之利用FastJson实现对象和Map互相转换*](https://blog.csdn.net/qq_41489540/article/details/109071292)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Java 对象转化为Map](https://blog.csdn.net/MYians/article/details/111241241)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Java Bean对象和Map相互转换.zip](https://download.csdn.net/download/qq_35461948/15483257)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文