问题
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
回答1:
--- 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.
来源:https://stackoverflow.com/questions/8227332/decoding-java-objects-in-python