
本文深入探讨了jedis客户端在调用`jsonget`方法时,将json中存储的字节数组值错误地解析为带有`.0`后缀的`double`类型的问题。我们将分析其根源在于底层json库的数值上转型机制,并提供三种实用的解决方案:包括手动类型转换、利用路径指定返回类型以及执行原始redis命令进行数据解析,旨在帮助开发者高效准确地处理redis json数据。
在使用Jedis客户端(特别是Jedis 4.2.3版本)与Redis进行交互时,开发者可能会遇到一个特定问题:当通过UnifiedJedis.jsonGet方法检索存储在Redis JSON中的字节数组数据时,返回的数值会带有.0的后缀,例如将原始字节值60解析为60.0。这表明Jedis在处理这些数值时,将其上转型为double类型,而非预期的byte或int类型。
此问题的核心在于Jedis 4.2.3版本内部使用的JSON处理库,如Gson和org.json:json。这些库在默认情况下,会将JSON字符串中的所有数字类型(无论是整数还是浮点数)统一解析为Java的double类型,以确保能够兼容最大范围的数值表示。因此,即使Redis中存储的是表示字节的整数值,经过这些库的解析后,也会被提升为double类型,从而导致输出中出现.0的后缀。
以下是一个典型的示例代码片段,展示了该问题:
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.UnifiedJedis;
public class JedisJsonGetExample {
public static void main(String[] args) {
HostAndPort config = new HostAndPort("localhost", 6379);
UnifiedJedis client = new UnifiedJedis(config);
// 假设Redis中键为"StandaloneResponse:..."的JSON数据中,
// ".variables.ReturnValue"路径下存储的是一个字节数组,例如 [60, 63, 120, ...]
Object object = client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782");
System.out.println(object);
// 输出中,ReturnValue字段会显示为 [60.0, 63.0, 120.0, ...]
client.close();
}
}针对这一问题,有多种策略可以确保获取到准确的字节值。以下是三种推荐的方法:
这种方法适用于当您不确定具体路径,或者需要处理整个JSON对象,并在获取后进行精细化控制的情况。jsonGet方法通常返回一个Object类型,当数据是JSON对象时,它会被解析为LinkedHashMap。您可以遍历这个Map,识别并转换需要纠正的double值。
实现思路: 将jsonGet返回的Object强制转换为LinkedHashMap,然后递归遍历其内容。当遇到ReturnValue等已知包含字节数组的字段时,将其中的double值转换为byte或int。
示例代码(概念性,需根据实际JSON结构调整):
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.UnifiedJedis;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
public class JedisJsonGetManualConversion {
public static void main(String[] args) {
HostAndPort config = new HostAndPort("localhost", 6379);
UnifiedJedis client = new UnifiedJedis(config);
try {
Object rawObject = client.jsonGet("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782");
if (rawObject instanceof LinkedHashMap) {
LinkedHashMap<String, Object> dataMap = (LinkedHashMap<String, Object>) rawObject;
// 假设ReturnValue在variables字段下
if (dataMap.containsKey("variables")) {
Object variablesObj = dataMap.get("variables");
if (variablesObj instanceof LinkedHashMap) {
LinkedHashMap<String, Object> variablesMap = (LinkedHashMap<String, Object>) variablesObj;
if (variablesMap.containsKey("ReturnValue")) {
Object returnValueObj = variablesMap.get("ReturnValue");
if (returnValueObj instanceof List) {
List<Object> rawList = (List<Object>) returnValueObj;
// 将List<Double>转换为List<Byte>或List<Integer>
List<Byte> byteList = rawList.stream()
.filter(item -> item instanceof Double)
.map(item -> ((Double) item).byteValue())
.collect(Collectors.toList());
variablesMap.put("ReturnValue", byteList); // 更新Map中的值
System.out.println("Converted ReturnValue: " + byteList);
}
}
}
}
System.out.println("Processed Object: " + dataMap);
} else {
System.out.println("Raw Object: " + rawObject);
}
} finally {
client.close();
}
}
}注意事项:
Jedis的jsonGet方法允许通过Path对象指定要检索的JSON路径,并且可以提供一个目标类来尝试直接反序列化。这是最推荐的方法,因为它简洁高效,并且能够利用Jedis的内置反序列化能力。
实现思路: 使用Path.of()指定到包含字节数组的精确路径,并提供Byte[].class作为期望的返回类型。Jedis会尝试将该路径下的数据直接反序列化为字节数组。
示例代码:
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path;
public class JedisJsonGetWithPathAndType {
public static void main(String[] args) {
HostAndPort config = new HostAndPort("localhost", 6379);
UnifiedJedis client = new UnifiedJedis(config);
try {
// 假设键为"StandaloneResponse:..."的JSON数据中,
// 字节数组位于".variables.ReturnValue"路径下
Byte[] returnValue = client.jsonGet(
"StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782",
Byte[].class,
Path.of(".variables.ReturnValue")
);
if (returnValue != null) {
System.out.print("Retrieved ReturnValue as Byte array: [");
for (int i = 0; i < returnValue.length; i++) {
System.out.print(returnValue[i]);
if (i < returnValue.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
} else {
System.out.println("ReturnValue not found or could not be deserialized.");
}
} catch (Exception e) {
System.err.println("Error retrieving data: " + e.getMessage());
e.printStackTrace();
} finally {
client.close();
}
}
}注意事项:
如果前两种方法无法满足需求,或者您需要对数据解析过程有极致的控制,可以直接执行Redis的原始JSON命令,并自行解析返回结果。这种方法绕过了Jedis内部的JSON库,直接获取Redis的原始响应。
实现思路: 使用client.executeCommand方法执行JSON.GET命令,该方法返回一个原始的Object,通常是byte[]。然后,您需要根据Redis响应的格式,手动将其转换为字符串,再进行JSON解析。
示例代码:
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.commands.Protocol.CommandArguments;
import redis.clients.jedis.json.JsonProtocol;
import com.google.gson.Gson; // 或其他JSON库,如Jackson
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class JedisJsonGetRawCommand {
public static void main(String[] args) {
HostAndPort config = new HostAndPort("localhost", 6379);
UnifiedJedis client = new UnifiedJedis(config);
Gson gson = new Gson(); // 用于手动解析JSON
try {
// 执行原始JSON.GET命令
Object rawResult = client.executeCommand(
new CommandArguments(JsonProtocol.JsonCommand.GET)
.add("StandaloneResponse:9b970b5f-32c2-4265-92cb-9af9d6707782")
);
if (rawResult instanceof byte[]) {
String jsonString = new String((byte[]) rawResult, StandardCharsets.UTF_8);
System.out.println("Raw JSON String: " + jsonString);
// 手动解析JSON字符串
// 注意:JSON.GET命令默认返回的是一个JSON数组,即使只查询一个路径
// 例如:["{\"exchangeId\":...}"]
List<String> jsonArray = gson.fromJson(jsonString, List.class);
if (!jsonArray.isEmpty()) {
String actualJsonContent = jsonArray.get(0); // 取出实际的JSON对象字符串
Map<String, Object> parsedMap = gson.fromJson(actualJsonContent, Map.class);
// 从解析后的Map中获取ReturnValue并进行转换
if (parsedMap.containsKey("variables")) {
Map<String, Object> variables = (Map<String, Object>) parsedMap.get("variables");
if (variables != null && variables.containsKey("ReturnValue")) {
List<Double> rawReturnValue = (List<Double>) variables.get("ReturnValue");
List<Byte> byteList = rawReturnValue.stream()
.map(Double::byteValue)
.collect(Collectors.toList());
System.out.println("Manually parsed ReturnValue as Byte list: " + byteList);
}
}
}
} else {
System.out.println("Unexpected raw result type: " + rawResult.getClass().getName());
}
} catch (Exception e) {
System.err.println("Error executing raw command: " + e.getMessage());
e.printStackTrace();
} finally {
client.close();
}
}
}注意事项:
在处理UnifiedJedis.jsonGet方法返回字节数组值带.0后缀的问题时,最佳实践是根据具体需求和对JSON结构的了解程度选择合适的解决方案:
理解Jedis底层JSON库的默认行为是解决此类问题的关键。通过选择合适的策略,可以确保从Redis中准确无误地获取和处理JSON数据。
以上就是解决UnifiedJedis中JSONGET方法返回字节数组值带.0后缀的问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号