简体   繁体   中英

Deserialize an object which has an array of arrays (Ajax call + C#)

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 .

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

On the client side: (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#)

    [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; }
    }

I have solved it evantually by changing the RequestObject to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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