If I have a two-dimensional array in C# - how can I convert it into a JSON string that contains a two dimensional array?
eg.
int[,] numbers = new int[8,4];
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(numbers);
gives a flat one-dimensional array in a JSON object. The Microsoft documentation states:
'A multidimensional array is serialized as a one-dimensional array, and you should use it as a flat array.'
You can use a jagged array instead of a two-dimensional array, which is defined like:
int[][] numbers = new int[8][];
for (int i = 0; i <= 7; i++) {
numbers[i] = new int[4];
for (int j = 0; j <= 3; j++) {
numbers[i][j] =i*j;
}
}
The JavascriptSerializer will then serialise this into the form [[#,#,#,#],[#,#,#,#],etc...]
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.