私は次のクラスを持っています:
public class TestQuestionHeader
{
public int Id { get; set; }
public int QId { get; set; }
}
および JSON メソッド:
public static string ToJSONString(this object obj)
{
using (var stream = new MemoryStream())
{
var ser = new DataContractJsonSerializer(obj.GetType());
ser.WriteObject(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
public static T FromJSONString<T>(this string obj)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(obj)))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
T ret = (T)ser.ReadObject(stream);
return ret;
}
}
私はこれらを次のように使用しています:
IList<TestQuestionHeaders> testQuestionHeaders = xxx;
string test.Questions = JSON.ToJSONString(testQuestionHeaders);
var a = JSON.FromJSONString<TestQuestionHeader>(test.Questions);
変数a
に TestQuestionHeader
_IList<TestQuestionHeader>