简单记录一下一个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&lt;String, String&gt; 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();
            }
        

结果
在这里插入图片描述

使用Jackson序列化

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进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注