json zhuan map
时间: 2023-11-08 07:17:32 浏览: 132
将JSON对象转为Map对象可以使用阿里巴巴封装的FastJSON来实现。你可以使用JSON类的parseObject方法来解析JSON字符串并转换为Map对象,或者使用JSONObject类的parse方法来实现。具体代码如下:
```java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
// 将JSON字符串解析为Map对象
String jsonString = "{\"key1\":\"value1\", \"key2\":\"value2\"}";
Map<String, Object> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Object>>() {});
// 遍历Map对象并输出键值对
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
```
这段代码可以将JSON字符串`{"key1":"value1", "key2":"value2"}`转换为Map对象,并通过遍历Map对象将键值对输出到控制台。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [如何把JSON对象转为map对象](https://blog.csdn.net/m0_54853503/article/details/124153413)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文