反序列化一个简单的JSON数组DataContractJsonSerializer数组、简单、序列化、JSON

2023-09-04 00:08:12 作者:何必锁我心

我敢肯定,这个问题已经被问了一遍又一遍,但由于某些原因,我还是不能设法得到这个工作。

I'm sure this question has been asked over and over again, but for some reason, I still can't manage to get this to work.

我要反序列化包含单个成员的JSON对象;一个字符串数组:

I want to deserialize a JSON object that contains a single member; a string array:

{"results" : ["a", "b"]}

这就是我想要反序列化到类:

This is the class that I'm trying to deserialize into:

public class Whatever {
    [DataMember(Name = "results")]
    public string[] Results { get; protected set; }
}

这是Deserialize方法:

And this is the Deserialize method:

private static T Deserialize<T>(string json)
{
    var instance = Activator.CreateInstance<T>();
    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
        var serializer = new DataContractJsonSerializer(instance.GetType());
        return (T)serializer.ReadObject(ms);
    }
}

一个电话像反序列化&LT;无论&GT;({\的结果\:\一\,\B \]})正在恢复