You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UnifiedJedis jsonGet方法返回字节数组值带.0的问题求助

问题:Jedis JSON获取数据时数值末尾带.0的解决方法

代码示例

HostAndPort config = new HostAndPort("localhost", 6379);
UnifiedJedis client = new UnifiedJedis(config);
Object object=client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782");
System.out.println(object);

输出结果

{exchangeId=9b970b5f-32c2-4265-92cb-9af9d6707782, createdTimestamp=Nov 23, 2022 4:14:01 PM, variables={OrderBy=, ErrorResponseMessage=, ErrorResponseCode=, Filter=, Expand=, Skip=0.0, Select=, Payload=[], ReturnValue=[60.0, 63.0, 120.0, 109.0, 108.0, 32.0, 118.0, 101.0, 114.0, 115.0, 105.0, 111.0, 110.0, 61.0, 34.0, 49.0, 46.0, 48.0, 34.0, 32.0, 101.0, 110.0, 99.0, 111.0, 100.0, 105.0, 110.0, 103.0, 61.0, 34.0, 73.0, 83.0, 79.0, 45.0, 56.0, 56.0, 53.0, 57.0, 45.0, 49.0, 34.0, 32.0, 115.0, 116.0, 97.0, 110.0, 100.0, 97.0, 108.0, 111.0, 110.0, 101.0, 61.0, 34.0, 110.0, 111.0, 34.0, 32.0, 63.0, 62.0, 13.0, 10.0, 60.0, 83.0, 116.0, 117.0, 100.0, 101.0, 110.0, 116.0, 32.0, 120.0, 109.0, 108.0, 110.0, 115.0, 58.0, 101.0, 100.0, 109.0, 61.0, 34.0, 104.0, 116.0, 116.0, 112.0, 58.0, 47.0, 47.0, 115.0, 99.0, 104.0, 101.0, 109.0, 97.0, 115.0, 46.0, 109.0, 97.0, 103.0, 105.0, 99.0, 115.0, 111.0, 102.0, 116.0, 119.0, 97.0, 114.0, 101.0, 46.0, 99.0, 111.0, 109.0, 47.0, 101.0, 100.0, 109.0, 34.0, 32.0, 120.0, 109.0, 108.0, 110.0, 115.0, 58.0, 110.0, 115.0, 49.0, 61.0, 34.0, 104.0, 116.0, 116.0, 112.0, 58.0, 47.0, 47.0, 115.0, 99.0, 104.0, 101.0, 109.0, 97.0, 115.0, 46.0, 109.0, 97.0, 103.0, 105.0, 99.0, 115.0, 111.0, 102.0, 116.0, 119.0, 97.0, 114.0, 101.0, 46.0, 99.0, 111.0, 109.0, 47.0, 101.0, 100.0, 109.0, 34.0, 32.0, 120.0, 109.0, 108.0, 110.0, 115.0, 58.0, 120.0, 115.0, 61.0, 34.0, 104.0, 116.0, 116.0, 112.0, 58.0, 47.0, 47.0, 119.0, 119.0, 119.0, 46.0, 119.0, 51.0, 46.0, 111.0, 114.0, 103.0, 47.0, 50.0, 48.0, 48.0, 49.0, 47.0, 88.0, 77.0, 76.0, 83.0, 99.0, 104.0, 101.0, 109.0, 97.0, 34.0, 32.0, 120.0, 109.0, 108.0, 110.0, 115.0, 58.0, 120.0, 115.0, 105.0, 61.0, 34.0, 104.0, 116.0, 116.0, 112.0, 58.0, 47.0, 47.0, 119.0, 119.0, 119.0, 46.0, 119.0, 51.0, 46.0, 111.0, 114.0, 103.0, 47.0, 50.0, 48.0, 48.0, 49.0, 47.0, 88.0, 77.0, 76.0, 83.0, 99.0, 104.0, 101.0, 109.0, 97.0, 45.0, 105.0, 110.0, 115.0, 116.0, 97.0, 110.0, 99.0, 101.0, 34.0, 62.0, 13.0, 10.0, 9.0, 60.0, 114.0, 111.0, 119.0, 62.0, 13.0, 10.0, 9.0, 9.0, 60.0, 70.0, 105.0, 101.0, 108.0, 100.0, 115.0, 62.0, 13.0, 10.0, 9.0, 9.0, 9.0, 60.0, 73.0, 68.0, 62.0, 52.0, 53.0, 60.0, 47.0, 73.0, 68.0, 62.0, 13.0, 10.0, 9.0, 9.0, 9.0, 60.0, 78.0, 97.0, 109.0, 101.0, 62.0, 116.0, 101.0, 115.0, 116.0, 32.0, 100.0, 97.0, 116.0, 97.0, 60.0, 47.0, 78.0, 97.0, 109.0, 101.0, 62.0, 13.0, 10.0, 9.0, 9.0, 60.0, 47.0, 70.0, 105.0, 101.0, 108.0, 100.0, 115.0, 62.0, 13.0, 10.0, 9.0, 60.0, 47.0, 114.0, 111.0, 119.0, 62.0, 13.0, 10.0, 60.0, 47.0, 83.0, 116.0, 117.0, 100.0, 101.0, 110.0, 116.0, 62.0], ReturnHTTPStatusCode=0.0}, status=0.0}

问题说明

Redis中存储的JSON数据数值类型为整数,但使用jedis-4.2.3调用jsonGet并以Object接收时,所有整数都被反序列化为带.0的Double类型,需要将这些数值恢复为整数形式。

解决思路

1. 定义实体类并指定类型反序列化

创建与Redis中JSON结构匹配的实体类,将需要整数的字段(如Skip、status、ReturnValue数组元素)定义为int或Integer类型,然后使用泛型版jsonGet方法直接反序列化:

// 示例实体类(需根据实际结构完善)
class ResponseEntity {
    private String exchangeId;
    private Date createdTimestamp;
    private Variables variables;
    private int status;

    static class Variables {
        private int Skip;
        private List<Integer> ReturnValue;
        // 其他字段...
        // getter/setter
    }
    // getter/setter
}

// 调用方式
ResponseEntity entity = client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782", ResponseEntity.class);
System.out.println(entity);

这种方式最规范,Jedis会根据实体类字段类型自动将JSON整数反序列化为整数。

2. 手动转换现有Object中的数值

若不想定义实体类,可将返回的Object转换为Map后遍历处理:

Map<String, Object> resultMap = (Map<String, Object>) client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782");

// 处理顶层status字段
if (resultMap.get("status") instanceof Double) {
    resultMap.put("status", ((Double) resultMap.get("status")).intValue());
}

// 处理variables中的Skip和ReturnValue
Map<String, Object> variables = (Map<String, Object>) resultMap.get("variables");
if (variables.get("Skip") instanceof Double) {
    variables.put("Skip", ((Double) variables.get("Skip")).intValue());
}

List<Object> returnValue = (List<Object>) variables.get("ReturnValue");
for (int i = 0; i < returnValue.size(); i++) {
    if (returnValue.get(i) instanceof Double) {
        returnValue.set(i, ((Double) returnValue.get(i)).intValue());
    }
}

System.out.println(resultMap);

该方法适合临时场景,需注意类型转换的安全性。

3. 配置Jedis的JSON反序列化规则

Jedis底层使用Jackson处理JSON反序列化,可通过配置ObjectMapper强制将JSON整数解析为整数类型:

// 获取Jedis的ObjectMapper实例并配置
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
// 将配置好的ObjectMapper注入Jedis
UnifiedJedis client = new UnifiedJedis(config);
client.setObjectMapper(objectMapper);

// 后续调用jsonGet即可自动解析整数
Map<String, Object> result = client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782", Map.class);

此方法全局生效,适合所有JSON反序列化场景。

内容的提问来源于stack exchange,提问作者Dattatray Satpute

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 14:25:19