反序列化具有数组数组的对象(Ajax调用+ C#)

时间:2020-12-14 21:32:50

Im having lots of difficulties deserializing an object with two properties, one is a simple string and the other one is an array of arrays (while every element in an array has a name and a value).

我有很多困难反序列化具有两个属性的对象,一个是简单的字符串,另一个是数组的数组(而数组中的每个元素都有一个名称和一个值)。

The POST operation works great until i reach the server side, i am able to debug there but can't deserialize the passed data correctly. I'm trying to deserialize the someData object to a RequestObject on the server side.

POST操作很有效,直到我到达服务器端,我能够在那里调试,但不能正确反序列化传递的数据。我正在尝试将someData对象反序列化为服务器端的RequestObject。

I would like to get your advice on this because im wasting my last hours bout that

我想得到你的建议,因为我浪费了我的最后几个小时

On the client side: (JavaScript)

在客户端:( JavaScript)

var dataArrays = [];
dataArrays[0] = [ {"name":"key","value":"xyz"} ];
dataArrays[1] = [ {"name":"firstname","value":"john"}, {"name":"lastname","value":"doe"} ];

var someData = {
someKey: "xyz",
dataArrays: dataArrays
};

$.ajax({
        type: 'POST',
        url: './mywebservice.asmx/Test',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(someData), ..);

On the server side: (C#)

在服务器端:(C#)

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Test(string someKey, string dataArrays)
    {
        // someKey has the value: "xyz"
        // dataArrays is a string in the following format:
        // [[{"name":"key","value":"xyz"}],[{"name":"firstname","value":"john"}{"name":"lastname","value":"doe"}]]

        //unable to deserialize in here in any way
        // ?? RequestObject requestObj = JsonConvert.DeserializeObject<RequestObject> ??

    }

    [DataContract]
    public class StringData
    {
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string value { get; set; }
    }

    [DataContract]
    public class RequestObject
    {
        [DataMember]
        public string someKey { get; set; }
        [DataMember]
        public List<StringData[]> dataArrays{ get; set; }
    }

1 个解决方案

#1


0  

I have solved it evantually by changing the RequestObject to

我已经通过将RequestObject更改为for来解决了这个问题

 [DataContract]
    public class RequestObject
    {
        [DataMember]
        public string someKey { get; set; }
        [DataMember]
       public List<List<StringData>> dataArrays{ get; set; }
    }

#1


0  

I have solved it evantually by changing the RequestObject to

我已经通过将RequestObject更改为for来解决了这个问题

 [DataContract]
    public class RequestObject
    {
        [DataMember]
        public string someKey { get; set; }
        [DataMember]
       public List<List<StringData>> dataArrays{ get; set; }
    }