序列化PHP =& gt;以字符串格式反序列化JAVA /针对php的序列化

编程入门 行业动态 更新时间:2024-10-26 15:25:41
本文介绍了序列化PHP =& gt;以字符串格式反序列化JAVA /针对php的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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 =&amp; gt;以字符串格式反序列化JAVA /针对php的序列化

本文发布于:2023-11-11 06:34:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   字符串   格式   PHP   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!