C#序列化不带属性名的JSON
也许以前有人问过这个问题,但我不知道如何寻找我的问题。 我正在使用WebAPI验证许可证。从这个API中,我得到了以下格式的返回JSON字符串C#序列化不带属性名的JSON,c#,json,serialization,C#,Json,Serialization,也许以前有人问过这个问题,但我不知道如何寻找我的问题。 我正在使用WebAPI验证许可证。从这个API中,我得到了以下格式的返回JSON字符串 string json = "[{"status":"error","status_code":"e002","message":"Invalid licence key"}]" 这是我的序列化程序 using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) {
string json = "[{"status":"error","status_code":"e002","message":"Invalid licence key"}]"
这是我的序列化程序
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ActivationResponseWooSl));
ActivationResponseWooSl ar = (ActivationResponseWooSl)js.ReadObject(ms);
}
现在我的问题是,“activationresponsewosl”类必须是什么样子,以便序列化程序可以转换它
非常感谢您的帮助
现在看起来是这样的(什么不起作用):
但是,当我序列化json字符串时,所有属性都是“null”。您的类是正确的。JSON是一个对象数组 试一试
这是一个快速而完美的解决方案!非常感谢您的快速支持:)@AndreasReitberger很高兴帮助您:)
[DataContract]
public class ActivationResponseWooSl
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "status_code")]
public string ErrorCode { get; set; }
[DataMember(Name = "message")]
public string ErrorMessage { get; set; }
}
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ActivationResponseWooSl[]));
var arr = (ActivationResponseWooSl[])js.ReadObject(ms);