我有一个在 Java 中转换为 ByteArray 的 HashMap。如何将 python 中的 ByteArray 转换为 python 中的映射,反之亦然?
HashMap 到 ByteArray 的转换代码
public static void main(String[] args) throws Exception {
// Create raw data.
Map<Integer, String> data = new HashMap<Integer, String>();
data.put(1, "hello");
data.put(2, "world");
System.out.println(data.toString());
// Convert Map to byte array
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(data);
// Parse byte array to Map
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
Map<Integer, String> data2 = (Map<Integer, String>) in.readObject();
System.out.println(data2.toString());
}
您可以尝试为像这样的 Java 序列化 API 产品找到一个解析器。但这听起来是个非常糟糕的主意。
要在不同的编程语言之间交换数据,请使用与平台和语言无关的序列化格式:XML、JSON、Protobuf、Thrift 等。
泡菜文档