且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

反序列化的JavaScriptSerializer对象"收缴"在对象属性失败

更新时间:2022-02-12 08:36:13

这是因为解串器不知道如何处理子对象。你有JS这是什么:

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

不具有地图的东西有效在C#:

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", ???);

该???是因为没有这里定义的类型,所以怎么会解串器知道在你的JS重新presents匿名对象吗?

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

修改

有没有办法做你试图在这里完成的。你需要让你的对象类型。例如,定义这样的类:

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.