我在php中有以下格式的数组:
I have an array in php of this format:
<?php $value = array("id" => 42, "user" => "superman"); echo serialize($value); ?>序列化:
a:2:{s:2:"id";i:42;s:4:"user";s:8:"superman";}我将其接收到Java中的 String 中。 如何在Java中反序列化呢? 我知道Java中的实现序列化的实现,但在这种情况下不起作用。
I receive this into a String in java. How I can do for deserialize this in java ? I know the implements Serializable in java but not work in this case.
我想创建这种格式的对象:
I want to create an object in this kind of format :
import java.io.Serializable; public class Serial implements Serializable{ private int mId; private String mUser; public Serial(int mId, String mUser) { super(); this.mId = mId; this.mUser = mUser; } public int getId() { return mId; } public void setId(int id) { this.mId = id; } public String getUser() { return mUser; } public void setUser(String user) { this.mUser = user; } }之后,我想创建另一个时间 String 从Java对象序列化以在PHP中反序列化;
After that I want to create another time the String serialized from the Java object for deserialize in PHP;
感谢您的帮助。
推荐答案您无法以另一种语言本机读取一种语言的序列化对象(每种语言都有自己的序列化协议/格式,无法保证它们可以读取一种语言,另一个格式),Java序列化的对象将序列化为二进制格式,PHP将您提供的文本格式序列化。
You can't natively read one languages serialised objects with another language (languages each have their own serialisation protocols/format, there is no guarantee they can read one-anothers format), Java serialised objects are serialised to a binary format and PHP your provided text format.
有些库例如 Google的协议缓冲区,它们并不正式支持PHP,但是有第三方库,它们为PHP提供协议缓冲支持。
There are libraries such as Google's protocol buffers that you can use, they don't officially support PHP, however there are 3rd party libraries which provide Protocol Buffer support for PHP.
协议缓冲区是Google的 la nguage-neutral ,平台中立,可扩展的用于序列化结构化数据的机制 –考虑使用XML,但是较小,更快,并且更简单。您定义了如何一次将数据构造为,然后可以使用生成的特殊源代码轻松地将写入和读取各种数据流中的结构化数据,并使用各种语言-Java,C ++或Python。
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.
如果您不喜欢这种语言,则需要开发自己的语言自己的用于将PHP的序列化对象读取为Java对象的协议,您也许可以使用经过修改的JSON库完成此操作,因为我不相信 s:2: id; 来自示例序列化对象的有效JSON。存在使用Java进行操作的库,但是PHP序列化格式不是't'safe',因为它可以包含空值,所以我建议不要这样做。
If you aren't fond of that, you will need to develop your own protocol for reading PHPs serialised objects into a Java object, you may be able to do this with a modified JSON library as I don't believe that s:2:"id"; from your sample serialised object is valid JSON. There exists a library for doing this using Java, however the PHP serialisation format isn't 'safe' as it can contain null values, so I would advise against it.
更多推荐
序列化PHP =& gt;以字符串格式反序列化JAVA /针对php的序列化
发布评论