在Xamarin服务上反序列化时,对象始终为空

时间:2018-06-25 20:01:49

标签: json xamarin service deserialization

已在xamarin应用程序上构建此服务:

public class OpenWeatherMap<T>
{
    private const string OpenWeatherApi = "http://api.openweathermap.org/data/2.5/weather?q=";
    private const string Key = "653b1f0bf8a08686ac505ef6f05b94c2";

    HttpClient _httpClient = new HttpClient();

    // aqui podemos enviar directo a una ciudad hardcoded
    public async Task<T> GetAllWeathers(string city)
    {
        var json = await _httpClient.GetStringAsync(OpenWeatherApi + city + "&APPID=" + Key);
        var getWeatherModels = JsonConvert.DeserializeObject<T>(json);
        return getWeatherModels;
    }
}

当我检查模型对象始终为空时。 JSON很好,我的课程与我的JSON模型完全匹配。我正在使用Newtonsoft.Json。

这是我的模特

public class WeatherMainModel
{

    [JsonProperty("coord")]
    public Coord Coord { get; set; }

    [JsonProperty("weather")]
    public WeatherSubDetails[] Weather { get; set; }

    [JsonProperty("base")]
    public string Base { get; set; }

    [JsonProperty("main")]
    public Main Main { get; set; }

    [JsonProperty("visibility")]
    public string Visibility { get; set; }

    [JsonProperty("wind")]
    public WeatherWindDetails Wind { get; set; }

    [JsonProperty("clouds")]
    public Clouds Clouds { get; set; }

    [JsonProperty("dt")]
    public string Dt { get; set; }

    [JsonProperty("sys")]
    public WeatherSysDetails Sys { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("cod")]
    public string Cod { get; set; }
}

public partial class Clouds // new
{
    [JsonProperty("all")]
    public string All { get; set; }
}

public partial class Coord // new
{
    [JsonProperty("lon")]
    public string Lon { get; set; }

    [JsonProperty("lat")]
    public string Lat { get; set; }
}

public partial class Main
{
    [JsonProperty("temp")]
    public string Temp { get; set; }

    [JsonProperty("pressure")]
    public string Pressure { get; set; }

    [JsonProperty("humidity")]
    public string Humidity { get; set; }

    [JsonProperty("temp_min")]
    public string TempMin { get; set; }

    [JsonProperty("temp_max")]
    public string TempMax { get; set; }
}

public partial class WeatherSysDetails
{
    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("country")]
    public string Country { get; set; }

    [JsonProperty("sunrise")]
    public string Sunrise { get; set; }

    [JsonProperty("sunset")]
    public string Sunset { get; set; }
}

public partial class WeatherSubDetails
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("main")]
    public string Main { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("icon")]
    public string Icon { get; set; }
}

public partial class WeatherWindDetails
{
    [JsonProperty("speed")]
    public string Speed { get; set; }

    [JsonProperty("deg")]
    public string Deg { get; set; }
}

我使用的是拉丁美洲(智利)的Visual Studio社区集,因此我尝试将JSON上的每个小数字段更改为模型上的字符串,以避免逗号分隔问题,但是无论如何,我的对象都为空我将JSON注入反序列化器。 预先感谢。

0 个答案:

没有答案