1

是否可以在 Windows 应用商店应用程序中使用本机DataContractJsonSerializer反序列化 json 数组?

示例,来自:

[{"groups":[{"name":"tom","vip":false},{"name":"sam","vip":true}]},{"groups":[{"name":"pam","vip":false},{"name":"mom","vip":true}]}]

到,大致在以下行中的任何内容:

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:

    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);
        }
    }
4

1 回答 1

4

所有的事情都是正确的。只需编写这些行即可获取对象数组。

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);
}
于 2013-05-27T12:54:17.760 回答