使用Windows Store App使用DataContractJsonSerializer对数组进行反序列化 [英] Deserialization of array with DataContractJsonSerializer with Windows Store App
本文介绍了使用Windows Store App使用DataContractJsonSerializer对数组进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Windows Store应用程序中使用本机 DataContractJsonSerializer 反序列化json数组?
Is it possible to deserialize a json array with native DataContractJsonSerializer in a Windows Store App?
示例,来自:
[{"groups":[{"name":"tom","vip":false},{"name":"sam","vip":true}]},{"groups":[{"name":"pam","vip":false},{"name":"mom","vip":true}]}]
收件人,大致符合以下条件的行:
To, anything roughly in the line of:
public class Group
{
public string name { get; set; }
public bool vip { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public List<Group> groups { get; set; }
}
到目前为止,我的尝试始终导致空列表或这样做时,IEnumerable为null。
So far, my attempts always resulted in a 'null' List or 'null' IEnumerable when doing it this way:
public static T deserializeJson<T>(string result)
{
DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result)))
{
ms.Position = 0;
return (T)jsonSer.ReadObject(ms);
}
}
推荐答案
全部事情是正确的。只需写这些行即可获取对象数组。
All the things are correct. Just write these line to get the object array.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string json = @"[{""groups"":[{""name"":""tom"",""vip"":false},{""name"":""sam"",""vip"":true}]},{""groups"":[{""name"":""pam"",""vip"":false},{""name"":""mom"",""vip"":true}]}]";
var res = deserializeJson<RootObject[]>(json);
//OR
var res1 = deserializeJson<List<RootObject>>(json);
}
这篇关于使用Windows Store App使用DataContractJsonSerializer对数组进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文