我想反序列化来自web服务的JSON,但由于某种原因,我遇到了一个异常。我搜索了与此主题相关的每个问题,但没有发现任何与此异常相关的问题。我可能有什么问题?这里有一个例外:
Newtonsoft.Json.JsonSerializationException: 'Unexpected token while deserializing object: EndObject. Path '', line 1, position 243.'
这是我的反序列化方法:
public async Task<LoginApiResponse> AuthenticateUserAsync(string username, string password)
{
// try
// {
LoginApiRequest loginRequest = new LoginApiRequest()
{
Username = username,
Password = password
};
// serialize object to json
var content = new StringContent(JsonConvert.SerializeObject(loginRequest), Encoding.UTF8, "application/json");
var response = await Client.PostAsync(Constants.LOGIN_URI, content);
response.EnsureSuccessStatusCode();
// get the data on success and serialize it from json
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>> " + Serializer.Deserialize<LoginApiResponse>(json));
return Serializer.Deserialize<LoginApiResponse>(json);
}
// }
/* catch (Exception ex)
{
return null;
}*/
}
这是请求模型:
public class LoginApiRequest
{
[JsonProperty("Username")]
public string Username { get; set; }
[JsonProperty("Password")]
public string Password { get; set; }
public LoginApiRequest() { }
}
这是响应模型:
public class LoginApiResponse : MessageStatus
{
[JsonProperty("Data")]
public User Data { get; set; }
public LoginApiResponse() { }
}
这是用户型号:
public class User
{
[JsonProperty("Address")]
public string Address { get; set; }
[JsonProperty("BloodType")]
public string BloodType { get; set; }
[JsonProperty("ConfirmPassword")]
public string ConfirmPassword { get; set; }
[JsonProperty("CustomerID")]
public string CustomerID { get; set; }
[JsonProperty("DOB")]
public string DOB { get; set; }
[JsonProperty("Email")]
public string Email { get; set; }
[JsonProperty("ID")]
public int? ID { get; set; }
[JsonProperty("IsActive")]
public bool IsActive { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Password")]
public string Password { get; set; }
[JsonProperty("Phone")]
public string Phone { get; set; }
[JsonProperty("UserRoleID")]
public int? UserRoleID { get; set; }
[JsonProperty("Username")]
public string Username { get; set; }
public User() { }
}
这是消息状态:
public class MessageStatus
{
[JsonProperty("Message")]
public string Message { get; set; }
[JsonProperty("Status")]
public int Status { get; set; }
public MessageStatus() { }
}
最后是json:
{
"Message": null,
"Status": 1,
"Data": {
"Address": "Beirut",
"BloodType": null,
"ConfirmPassword": null,
"CustomerID": null,
"DOB": null,
"Email": null,
"ID": 22,
"IsActive": true,
"Name": "tg",
"Password": "123456",
"Phone": "03708424",
"UserRoleID": 1,
"Username": "tg"
}
}
而不是
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>> " + Serializer.Deserialize<LoginApiResponse>(json));
return Serializer.Deserialize<LoginApiResponse>(json);
}
尝试
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<LoginApiResponse>(json)
return data;