Xamarin 表单 Json 反序列化

Xamarin Forms Json Desrialization

我想反序列化来自 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;