我想反序列化一个包含单个成员的JSON对象;一个字符串数组:使用C#中的DataContractJsonSerializer以List <>反序列化JSON对象
{"errores":[{"errorCode":"campo1","errorMessage":"Errorenelcampo1"},{"errorCode":"campo2","errorMessage":"Errorenelcampo2"},{"errorCode":"campo3","errorMessage":"Errorenelcampo3"},{"errorCode":"campo4","errorMessage":"Errorenelcampo4"}]}"
这是我尝试反序列化到类:
[DataContract]
internal class rptaNo
{
[DataMember]
public List<Errors> errores { get; set; }
[DataContract]
protected internal struct Errors
{
[DataMember]
public string codigo { get; set; }
[DataMember]
public string descripcion { get; set; }
}
}
这是我尝试反序列化方法:
public T Deserialise<T>(string json)
{
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
T result = (T)deserializer.ReadObject(stream);
return result;
}
}
但是,当我检查,子列表的所有字段都是空白的,例如
MessageBox.Show(deserializedRpta2.errores[0].descripcion);
我想一定是我丢失的东西=(
你有没有尝试'System.Web.Script.Serialization。 JavascriptSerializer'? – Olrac
[在C#中使用DataContractJsonSerializer反序列化JSON对象](http://stackoverflow.com/questions/17409655/deserialization-of-json-object-by-using-datacontractjsonserializer-in-c-sharp) –