JavaScriptSerializer 反序列化对象“集合”作为对象失败的属性

发布于 2024-09-04 03:19:26 字数 446 浏览 8 评论 0 原文

我有一个 js 对象,其结构如下:

object.property1 = "some string";
object.property2 = "some string";
object.property3.property1 = "some string";
object.property3.property2 = "some string";
object.property3.property2 = "some string";

我正在使用 JSON.stringify(object) 通过 ajax 请求传递它。当我尝试使用 JavaScriptSerializer.Deserialize 作为字典对其进行反序列化时,出现以下错误:

没有为“System.String”类型定义无参数构造函数。

这个完全相同的过程适用于具有非“集合”属性的常规对象..感谢您的帮助!

I have a js object structured like:

object.property1 = "some string";
object.property2 = "some string";
object.property3.property1 = "some string";
object.property3.property2 = "some string";
object.property3.property2 = "some string";

i'm using JSON.stringify(object) to pass this with ajax request. When i try to deserialize this using JavaScriptSerializer.Deserialize as a Dictionary i get the following error:

No parameterless constructor defined for type of 'System.String'.

This exact same process is working for regular object with non "collection" properties.. thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黄昏下泛黄的笔记 2024-09-11 03:19:26

这是因为反序列化器不知道如何处理子对象。 JS 中的内容是这样的:

var x = {
  'property1' : 'string',
  'property2' : 'string',
  'property3' : { p1: 'string', p2: 'string', p3: 'string' },
};

它没有到 C# 中有效内容的映射:

HashTable h = new HashTable();
h.Add("property1", "string");
h.Add("property2", "string");
h.Add("property3", ???);

???是因为这里没有定义类型,那么反序列化器如何知道JS中的匿名对象代表什么?

编辑

无法完成您在此处想要完成的任务。您需要输入对象。例如,像这样定义你的类:

class Foo{
  string property1 { get; set; } 
  string property2 { get; set; }
  Bar property3 { get; set; } // "Bar" would describe your sub-object
}

class Bar{
  string p1 { get; set; }
  string p2 { get; set; }
  string p3 { get; set; }
}

...或类似的东西。

It's because the deserializer doesn't know how to handle the sub-object. What you have in JS is this:

var x = {
  'property1' : 'string',
  'property2' : 'string',
  'property3' : { p1: 'string', p2: 'string', p3: 'string' },
};

which doesn't have a map to something valid in C#:

HashTable h = new HashTable();
h.Add("property1", "string");
h.Add("property2", "string");
h.Add("property3", ???);

The ??? is because there's no type defined here, so how would the deserializer know what the anonymous object in your JS represents?

Edit

There is no way to do what you're trying to accomplish here. You'll need to make your object typed. For example, defining your class like this:

class Foo{
  string property1 { get; set; } 
  string property2 { get; set; }
  Bar property3 { get; set; } // "Bar" would describe your sub-object
}

class Bar{
  string p1 { get; set; }
  string p2 { get; set; }
  string p3 { get; set; }
}

...or something to that effect.

残月升风 2024-09-11 03:19:26

作为一个更一般的答案,在我的例子中,我的对象看起来像:

{ "field1" : "value", "data" : { "foo" : "bar" } }

我最初将数据字段作为字符串,而它应该是 Dictionary href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow">MSDN 用于使用字典语法的对象。

public class Message
{
   public string field1 { get; set; }

   public Dictionary<string, string> data { get; set; }
}

As a more general answer, in my case I had objects that looked like:

{ "field1" : "value", "data" : { "foo" : "bar" } }

I originally had the data field as a string when it should've been a Dictionary<string, string> as specified on MSDN for objects that use dictionary syntax.

public class Message
{
   public string field1 { get; set; }

   public Dictionary<string, string> data { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文