【发布时间】:2021-07-02 13:43:14
【问题描述】:
我想反序列化来自 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"
}
}
【问题讨论】:
-
您是否肯定是您的应用收到的实际 json 响应,并且没有多余的字符?您是否尝试过读取为字符串并反序列化而不是使用流?
-
@Jason 是的,它必须是它,我通过从我的代码中调用相同的 url 从邮递员那里得到了这个 json,我怎样才能得到 json 作为字符串来检查?
-
使用
ReadAsStringAsync() -
@Jason ok 会检查并回复您 :)
-
@Json 出现此异常:Newtonsoft.Json.JsonSerializationException: '将值“消息”转换为类型'DeliveryApplication.Models.Login.LoginApiResponse' 时出错。路径'',第 1 行,位置 10。'
标签: json xamarin serialization xamarin.forms mobile