简体   繁体   中英

Serializing and Deserializing a Jagged Array C#

I have a method in my web service which runs two linq queries and inserts the results into a Jagged Array. I am now attempting to serialize and deserialize the array. I am using newtonsoft json to do so.

however when I try to deserialize the array I get the following:

Additional information: Could not create an instance of type System.Array. Type is an interface or abstract class and cannot be instantiated. Path '[0] [0]', line 1, position 3.

In my webservice method I have (Serializing):

Array[][] myArray = new Array[2][];
myArray[0] = mtsections; //linq query 1
myArray[1] = sectionDetails; //linq query 2

string myp = JsonConvert.SerializeObject(myArray);

return myp;

And in my client i have (Deserializing):

string JSONString = context.ExpordOfQuestionsWord(RaId);

Array[][] myArray = new Array[2][];

myArray = JsonConvert.DeserializeObject<Array[][]>(JSONString);

Anybody know whats going on?

Thanks.

This is due to the fact that deserialization is performed by first creating objects using default constructors and then filling internal values. Here you are using Array class which is an abstract one. Thus you cannot create instances of it.

If you want to use two arrays of different type then simple solution would be to send two jsons.

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