简体   繁体   English

如何:从JavaScript反序列化C#对象

[英]How to : Deserialize a C# object from javascript

I have searched a bit online and couldn't find an answer to my question. 我已经在网上进行了一些搜索,找不到我的问题的答案。

I have a C# serialization that is made from a MemoryStream's Object. 我有一个从MemoryStream的Object进行的C#序列化。 I would like to save that Serialization (which is a thing I already can do) and then deserialize it with JavaScript. 我想保存该序列化(这是我已经可以做的事情),然后使用JavaScript反序列化。

Do anyone know if this is possible ? 有人知道这是否可能吗? I haven't seen any API which could do that. 我还没有看到任何可以做到这一点的API。

Thank you for your answers. 谢谢您的回答。 To be more specific I already have an application running in C# which use the MemoryStream's deserialization. 更具体地说,我已经有一个在C#中运行的应用程序,它使用MemoryStream的反序列化。 This is a pretty big application and I want to do as few modifications as possible. 这是一个很大的应用程序,我想做的修改尽可能少。 I want to link an other application (which is running in HTML/JavaScript) with the first one by using the same serialization. 我想通过使用相同的序列化将另一个应用程序(在HTML / JavaScript中运行)与第一个应用程序链接。 If I use the JSON Serialization I will have to often modify the code in C# which is a thing I want to try to avoid. 如果我使用JSON序列化,我将不得不经常修改C#中的代码,这是我想避免的事情。

To summarize, I want to be able to "read" the serialization generated by C# in my javascript project. 总而言之,我希望能够“读取”我的JavaScript项目中C#生成的序列化。

In order to be clear I don't want to use an other serialization to JSON / XML / ..., I would like to use the one I'm already using and then deserialize it with JavaScript. 为了清楚起见,我不想对JSON / XML / ...使用其他序列化,我想使用我已经在使用的序列化,然后用JavaScript反序列化。

Thanks in advance. 提前致谢。

When you use Serialization in C# you usually have Binary or XML-Serialization. 在C#中使用序列化时,通常具有二进制或XML序列化。 None of those can be read by JavaScript out of the box. 开箱即用的JavaScript都不可读。 (And it really is not worth the effort to try to implement this). (并且实在不值得尝试实现此目标)。

But switching from object serialization to JSON and (vice versa) is not really hard: 但是从对象序列化切换到JSON,反之亦然并不困难:

Person p = new Person();
p.name = "John";
p.age = 42;
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);

(example from MSDN: https://msdn.microsoft.com/en-us/library/bb412179%28v=vs.110%29.aspx ) (来自MSDN的示例: https : //msdn.microsoft.com/zh-cn/library/bb412179%28v=vs.110%29.aspx

So. 所以。 In Short: Rethink your requirements . 简而言之: 重新考虑您的要求 You won't find an easy solution without Json 没有Json,您将找不到简单的解决方案

You could Either try XML Serialization or JSON. 您可以尝试XML序列化或JSON。 C# has an XML provider class wich is easy to use, but you should parse your data as object Array, so Java and C# can deserialize them in the way like: C#具有一个易于使用的XML提供程序类,但是您应该将数据解析为对象Array,因此Java和C#可以采用以下方式反序列化它们:

public class person
{
  public string name;
  public byte age;
  public person(object[] args)
  {
     name = args[0];
     age = args[1];
  }
}

On Recieve you could deserialize in an objec[] and then create an Class Instance with 在Recieve上,您可以在objec []中反序列化,然后创建具有以下内容的类实例

Person p = new Person(rec_args);

I dont exactly know how Javascript handels classes but on Serilaziation level, you have to work on basic level (Only Serialize variables into object Arrays and Reverse) to have a clean bugfree execution! 我不完全知道Javascript是如何处理类的,但是在Serilaziation级别上,您必须在基本级别上工作(仅将变量序列化为对象Array和Reverse)才能执行干净的无错误操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM