反序列化的JavaScriptSerializer对象"收缴"在对象属性失败 [英] JavaScriptSerializer deserialize object "collection" as property in object failing

查看:144
本文介绍了反序列化的JavaScriptSerializer对象"收缴"在对象属性失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JS对象结构类似:

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

即时透过JSON.stringify(对象)与Ajax请求通过这个。当我尝试反序列化用这个作为JavaScriptSerializer.Deserialize一个解释,我得到了以下错误:

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:

没有参数的构造函数的类型'System.String'定义的。

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!

推荐答案

这是因为解串器不知道如何处理子对象。你有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#:<​​/ P>

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.

这篇关于反序列化的JavaScriptSerializer对象&QUOT;收缴&QUOT;在对象属性失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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