简单记录一下一个fastjson框架和jackson进行序列化的使用案例:
原json字符串:
“{“lockCount”:”{1:790,113:1,2:0,211:0,101:1328,118:8,137:0,301:0,302:0}“,“inventoryCount”:”{1:25062,113:2,2:10000,211:2,101:11034,118:9,137:40,301:903914,302:2070}“}”;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
String jsonString = "{"lockCount":"{1:790,113:1,2:0,211:0,101:1328,118:8,137:0,301:0,302:0}","inventoryCount":"{1:25062,113:2,2:10000,211:2,101:11034,118:9,137:40,301:903914,302:2070}"}";
try {
Map<String, String> resultMap = JSON.parseObject(jsonString, new TypeReference<Map<String, String>>() {});
// 获取 lockCount 和 inventoryCount 的值
String lockCountJson = resultMap.get("lockCount");
String inventoryCountJson = resultMap.get("inventoryCount");
// 转换为相应的 Map 对象
Map<Integer, Integer> lockCountMap = JSON.parseObject(lockCountJson, new TypeReference<Map<Integer, Integer>>() {});
Map<Integer, Integer> inventoryCountMap = JSON.parseObject(inventoryCountJson, new TypeReference<Map<Integer, Integer>>() {});
System.out.println("lockCountMap: " + lockCountMap);
System.out.println("inventoryCountMap: " + inventoryCountMap);
} catch (Exception e) {
e.printStackTrace();
}
结果:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class JsonToMapExample {
public static void main(String[] args) {
String jsonString = "{"lockCount":"{1:790,113:1,2:0,211:0,101:1328,118:8,137:0,301:0,302:0}","inventoryCount":"{1:25062,113:2,2:10000,211:2,101:11034,118:9,137:40,301:903914,302:2070}"}";
try {
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {};
Map<String, String> resultMap = objectMapper.readValue(jsonString, typeReference);
// 获取 lockCount 和 inventoryCount 的值
String lockCountJson = resultMap.get("lockCount");
String inventoryCountJson = resultMap.get("inventoryCount");
// 转换为相应的 Map 对象
Map<Integer, Integer> lockCountMap = objectMapper.readValue(lockCountJson, new TypeReference<Map<Integer, Integer>>() {});
Map<Integer, Integer> inventoryCountMap = objectMapper.readValue(inventoryCountJson, new TypeReference<Map<Integer, Integer>>() {});
System.out.println("lockCountMap: " + lockCountMap);
System.out.println("inventoryCountMap: " + inventoryCountMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址:https://blog.csdn.net/qq_41358574/article/details/134638584
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_13109.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。