反序列化JSON并断言响应



本质上,我试图编写一个测试来反序列化对Customer对象的JSON响应,并断言Firstname是";Darron";

JSON对象

{
"Id": 615,
"Customer": {
"Id": 726,
"MemberNodeId": 2257,
"EmailAddress": "Darron_Lips@email.com",
"Deleted": false,
"LastModifiedBy": "CUSTOMER",
"LastModifiedOn": "2021-06-02T08:54:38.243Z",
"CreatedBy": "CUSTOMER",
"CreatedOn": "2021-06-02T08:54:38.243Z"
},
"Title": {
"Id": 1,
"Title": "Mr"
},
"Firstname": "Darron",
"Lastname": "Lips",
"Gender": "Male",
"DateOfBirth": "1977-11-21T00:00:00Z",
"Height": 241.000000000,
"Weight": 204.000000000,
"LastModifiedBy": "CUSTOMER",
"LastModifiedOn": "2021-06-02T08:54:38.257Z",
"CreatedBy": "CUSTOMER",
"CreatedOn": "2021-06-02T08:54:38.257Z"
}
]

客户型号

public class Customer
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("MemberNodeId")]
public int MemberNodeId { get; set; }
[JsonProperty("EmailAddress")]
public string EmailAddress { get; set; }
[JsonProperty("Deleted")]
public bool Deleted { get; set; }
[JsonProperty("LastModifiedBy")]
public string LastModifiedBy { get; set; }
[JsonProperty("LastModifiedOn")]
public DateTime LastModifiedOn { get; set; }
[JsonProperty("CreatedBy")]
public string CreatedBy { get; set; }
[JsonProperty("CreatedOn")]
public DateTime CreatedOn { get; set; }
}
public class CustomerTitle
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("Title")]
public string Title { get; set; }
}
public class Root
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("Customer")]
public Customer Customer { get; set; }
[JsonProperty("Title")]
public string Title { get; set; }
[JsonProperty("Firstname")]
public string Firstname { get; set; }
[JsonProperty("Lastname")]
public string Lastname { get; set; }
[JsonProperty("Gender")]
public string Gender { get; set; }
[JsonProperty("DateOfBirth")]
public DateTime DateOfBirth { get; set; }
[JsonProperty("Height")]
public int Height { get; set; }
[JsonProperty("Weight")]
public int Weight { get; set; }
[JsonProperty("LastModifiedBy")]
public object LastModifiedBy { get; set; }
[JsonProperty("LastModifiedOn")]
public DateTime LastModifiedOn { get; set; }
[JsonProperty("CreatedBy")]
public string CreatedBy { get; set; }
[JsonProperty("CreatedOn")]
public DateTime CreatedOn { get; set; }
}
}

和测试

[Test]
public void CanGetCustomerDetails()
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("CustomerDetailsApi/GetAll", Method.GET);
request.AddHeader("Authorization", $"Bearer {token}");
IRestResponse response = client.Execute(request);

}

到目前为止,我已经试过

var customer = client.Execute(request).Content;
var responsea = JsonConvert.DeserializeObject<List<Customer>>(customer);
Assert.AreEqual(responsea, "firstname");

var customer = client.Execute(request).Content;
var responsea = JsonConvert.DeserializeObject<Customer>(customer);

结果是:

"Newtonsoft.Json.JsonSerializationException:"无法将当前Json数组(例如[1,2,3](反序列化为类型";

您的JSON与您的类不匹配。首先,您应该取消对List<Root>的序列化,但也需要修改该类,使其具有Title属性的子类。此外,不能将"高度"one_answers"重量"的这些数字取消序列化为int属性。

首先,你的Root类是吗

public class Root
{
public int Id { get; set; }
public Customer Customer { get; set; }
public CustomerTitle Title { get; set; } // Correct type used here
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public double Height { get; set; } // double rather than int
public double Weight { get; set; } // double rather than int
public object LastModifiedBy { get; set; }
public DateTime LastModifiedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
}

现在取消序列化为正确的类型:

var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);

现在你可以断言这样的东西:

Assert.AreEqual(responsea[0].Firstname, "Darron");

请注意,如果属性名称已经匹配,则不需要所有JsonProperty属性。

对我有效的方法只是将我的响应作为";"内容";如下所示:

var responsea = JsonConvert.DeserializeObject<List<Root>>(customer.Content);

试试这个:

var customer = client.Execute(request).Content;
var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);
Assert.AreEqual(responsea[0].Firstname , "Darron");

本质上,responsea[0].Firstname访问List的第一个元素,获取其Firstname属性并使用值断言,该值似乎是"Darron"

您的第二次尝试,实质上是尝试将整个数组转换为引发异常的一个对象。

此外,您有一些声明的类型与您的JSON不匹配。

相关内容

  • 没有找到相关文章

最新更新