解码python中的Java对象 [英] Decoding java objects in python

查看:284
本文介绍了解码python中的Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在python中解码Java对象?我知道返回数据的java类的结构.返回的数据带有"Transfer-Encoding:chunked"的标题

还是我需要获取以xml/json格式发送的返回数据?

这是我得到回应的一个例子; (请注意,某些控制字符会通过粘贴删除)

 -- response --
200 OK
Server:  Apache-Coyote/1.1
Transfer-Encoding:  chunked
Date:  Tue, 22 Nov 2011 13:24:41 GMT
��srjava.util.ArrayListx����a�IsizexpGwXsr,com.blah.blah.data.ClientInfos����8d�LdbNametLjava/lang/String;Ldescriptionq~xptdbname1t Customername1q~tdbname2tCustomername2sq~t

此示例以客户端的格式(数据库名称)返回两个客户端

dbname1,customername1

dbname2,customername2

解决方案

---随着更多详细信息可用而进行编辑---

此HTTP响应的内容是序列化的Java对象.

Java序列化是一个过程,通过该过程,内存中的对象被打包为便于传输的字节,以便被另一个JVM读取.简而言之,您需要阅读序列化格式.假设他们没有添加自定义序列化程序,则实际协议记录在这里.

其余的只是确保您对数据字段进行了明智的Java到Python的映射.如果要支持往返"数据处理,则应将有时丢弃的"java信息"缓存在数据结构的特殊区域"中,以便可以序列化回相同的java构造.

---原始帖子紧随其后---

要么正在分解一个类(这很容易做到,因为Java具有非常严格的类结构,并且易于将字节码反向转换为源代码映射),或者正在处理数据.

如果它正在处理数据,那么您实际上并没有在解码Java,而执行所需操作的难易程度在很大程度上取决于收集有关如何编码数据的知识的能力.由于您提到了"Transfer-Encoding:Chunked"标头,所以我猜您正在读取HTTP响应(标头完整).

由于Web服务的普及,几乎每种语言都有一个库来处理HTTP响应. Python已经具有此处所示的HTTP客户端. /p>

Is it possible to decode java objects in python? I know the structure of the java class that returns the data. The data is returned with the header of 'Transfer-Encoding: chunked'

Or do I need to get the return data sent as xml / json?

This is an example of the response I get; (Note some of the control characters are removed by pasting)

 -- response --
200 OK
Server:  Apache-Coyote/1.1
Transfer-Encoding:  chunked
Date:  Tue, 22 Nov 2011 13:24:41 GMT
��srjava.util.ArrayListx����a�IsizexpGwXsr,com.blah.blah.data.ClientInfos����8d�LdbNametLjava/lang/String;Ldescriptionq~xptdbname1t Customername1q~tdbname2tCustomername2sq~t

This example returns two clients in the format of client, databasename

dbname1,customername1

dbname2,customername2

解决方案

--- Edited as more details became available ---

The content of this HTTP response is a serialized Java object.

Java serialization is a process by which an in-memory object gets packed into transport-friendly bytes for the purpose of being read by another JVM. In short, you need to read the serialization format. Assuming that they didn't add a custom serializer, the actual protocol is documented here.

The rest is just assuring that you do a sensible Java to Python mapping of the data fields. If you want to support "round trip" data handling, you should cache the sometimes discarded "java information" in special "areas" of your data structure so you can serialize back to the same java constructs.

--- Original Post follows ---

Either you are disassembling a class (which is easy to do as Java has a very rigid class structure and an easy to reverse byte code to source code mapping) or you are processing data.

If it is processing data, you're not really decoding Java, and the ease of doing what you want depends heavily on the ability to gather knowledge about how the data is encoded. Since you mentioned a "Transfer-Encoding: Chunked" header, I am guessing you are reading a HTTP response (complete with headers).

Nearly every language has a library to handle HTTP responses due to the popularity of web services. Python already has a HTTP client as indicated here.

这篇关于解码python中的Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆