简体   繁体   English

反序列化复杂对象

[英]Deserialize complex object

I'm trying to deserialize an object sent by the browser. 我正在尝试反序列化浏览器发送的对象。 My object is array of Detail with the name as key. 我的对象是名称为键的Detail数组。 The name is a string and the detail an object with properties. 名称是字符串,详细信息是具有属性的对象。

this is Picture of the javascript object: 这是javascript对象的图片:

JavaScript对象的图片

And this is the JSON String I receive, created with "JSON.stringify(TemplateDetails)": 这是我收到的JSON字符串,它是使用“ JSON.stringify(TemplateDetails)”创建的:

"{\"UDF1-0-div\":{\"UDFtitle\":\"theTitle\",\"DDLType\":\"STRING\",\"defaultValue\":\"defVal\",\"minLength\":\"1\",\"maxLength\":\"6\",\"decimals\":\"\",\"DDLTable\":\"\",\"DDLFilter\":\"\",\"DDLAction\":\"TEST\",\"DDLfontfamily\":\"Verdana\",\"DDLSize\":\"12px\",\"DDLTextAlignment\":\"left\",\"colorTitle\":\"#FFFFFF\",\"colorText\":\"#FFFFFF\"}}"

I want to recreate the object in the c# code. 我想在C#代码中重新创建对象。

First of all you should create a class with all of properties you need : 首先,您应该创建一个具有所需所有属性的类:

   public class MyClass
    {
    public string DDLAction{ get; set; }
    public string DDLFilter{ get; set; }
    public string DDLSize{ get; set; }

    // put all of your attributes
    //...
    }

And for deserialization : 对于反序列化:

System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            MyClass Obj = ser.Deserialize<MyClass>(input);

That's not a dictionary, a dictionary looks like this: 那不是字典,字典看起来像这样:

{
    "objName": {
        ["title", "thetitle"], 
        ["DDLType":"STRING"], 
        ["defaultValue": "defVal"]
    }
}

You got a regular object and will have to create it as such. 您有一个常规对象,因此必须创建它。

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

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