序列化PHP =>反序列化JAVA /序列化字符串格式的PHP [英] Serialize PHP => Unserialize JAVA / Serialize for php in string format

查看:188
本文介绍了序列化PHP =>反序列化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中这一点。
我怎么可以在Java的反序列化做到这一点?
我知道在java中的实现Serializable ,但在这种情况下无​​法正常工作。

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;
    }
}

之后,我想创建另一个时间字符串从Java对象在PHP反序列化系列化;

After that I want to create another time the String serialized from the Java object for deserialize in PHP;

感谢您的帮助。

推荐答案

您无法读取本地序列化对象语言之一用另一种语言(语言都有自己的系列化协议/格式,也不能保证他们可以阅读单anothers格式)的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.

有库,如谷歌的协议缓冲区的,你可以使用,只要不正式支持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.

协议缓冲区是谷歌的中性语言 平台中立
  可扩展的机制序列化结构化数据 - XML,但是
  更小,更快,更简单。你可以定义你希望你的数据是
  结构化的一次,那么你可以使用特殊的生成的源$ C ​​$ C到
  轻松写入和读取你的结构化数据和从各种
  数据流,并使用各种语言 - 的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库要做到这一点,因为我不相信取值:2:身份证; 从你的样品序列化对象是有效的JSON。存在一个这样做使用Java,但是PHP序列化格式不安全的,因为它可以包含空值,所以我会建议反对。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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