using System.IO;
using System.Runtime.Serialization.Json;
class Program
{
static void Main(string[] args)
{
string [][] testArray = new string[1][];
testArray[0] = new string[2];
testArray[0][0] = "2";
testArray[0][1] = "濮阳乐荷";
// 构造 序列化类.
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(testArray.GetType());
MemoryStream s = new MemoryStream();
// 二维数组 --> Json 字符串
dcjs.WriteObject(s, testArray);
string jsonString = System.Text.Encoding.UTF8.GetString(s.ToArray());
Console.WriteLine(jsonString);
Stream s2 = new MemoryStream( System.Text.Encoding.UTF8.GetBytes ( jsonString ));
// Json 字符串 --〉二维数组
string[][] testArray2 = (string[][])dcjs.ReadObject(s2);
Console.WriteLine(testArray2.Length);
Console.WriteLine(testArray2[0].Length);
Console.WriteLine(testArray2[0][0]);
Console.WriteLine(testArray2[0][1]);
Console.ReadLine( );
}
}
运行结果:
[["2","濮阳乐荷"]]
1
2
2
濮阳乐荷