Api Climatempo C#

Asked

Viewed 1,018 times

1

Good afternoon, I am making a web application for college where I need to consume a C#Climatempo API, in my code, I am passing as parameter the code of the city and making the request for the: https://advisor.climatempo.com.br

This is my request URL at the end: http://apiadvisor.climatempo.com.br/api/v1/weather/locale/3480/current?token=866474fc3c51b2f4229db9d8f11648de

So far so good, I created the fields in VIEW to inform the city code and Controller to search:

 public ActionResult PrevisaoTempo(UsuarioCliente usuario)
 {

            string url = "http://apiadvisor.climatempo.com.br/api/v1/weather/locale/"+usuario.Cod+"/current?token=866474fc3c51b2f4229db9d8f11648de";
            WebClient client = new WebClient();
            string json = client.DownloadString(url);
            byte[] bytes = Encoding.Default.GetBytes(json);
            json = Encoding.UTF8.GetString(bytes);

            usuario = JsonConvert.DeserializeObject<UsuarioCliente>(json);

            TempData["Usuario"] = usuario;

            return RedirectToAction("Create", "PrevisaoTempo");
  }

The point is, it brings JSON all right and I can even show in the view the information behind JSON’s "DATA". But after DATA, it becomes NULL after going through the Deserializeobject and so the temperature and Sensation fields arrive null’s in the VIEW, can you help me with that? Thank you very much!

In case I just want to bring the temperature and Sensation of the DATA vector User class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace ProjetoCotacao2.Models
{
    [Table("UsuarioClientes")]
    public class UsuarioCliente
    {

        [Key]
        public int IDUsuarioCliente { get; set; }

        [Display(Name = "E-mail")]
        [EmailAddress(ErrorMessage = "E-mail inválido!")]
        public string Email_Cliente { get; set; }

        [Required(ErrorMessage = "Campo obrigatório!")]
        [Display(Name = "Login do usuário")]
        public string Login { get; set; }

        [Display(Name = "Senha")]
        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Campo obrigatório!")]
        public string SenhaCliente { get; set; }


        [Display(Name = "Confirmação Senha")]
        [Compare("SenhaCliente", ErrorMessage = "Os campos não coincidem!")]
        [DataType(DataType.Password)]
        [NotMapped]

        public string ConfirmacaoSenha { get; set; }

        [Display(Name = "CPF")]

        [Required(ErrorMessage = "Campo obrigatório!")]
        public string CPF_Cliente { get; set; }

        [Display(Name = "Nome")]
        [MinLength(3, ErrorMessage = " No mínimo 3 caracteres")]
        [Required(ErrorMessage = "Campo obrigatório!")]
        public string Nome_Cliente { get; set; }

        [Display(Name = "Telefone")]
        [Required(ErrorMessage = "Campo obrigatório!")]
        public string Telefone_Cliente { get; set; }

        public string Cep { get; set; }
        public string Logradouro { get; set; }
        public string Localidade { get; set; }


        public string Name { get; set; }
        public string Id { get; set; }
        public string State { get; set; }
        public string temperature { get; set; }
        public string sensation { get; set; }


    }
}
  • Poste a class Usuariocliente

  • I updated the post

  • your class is quite different from what json returns

  • The class belongs to a larger project, the Climate API is part of the project

2 answers

2

Your class UsuarioCliente is not the same as the properties of Json. To ensure the mapping of all property use this site: http://json2csharp.com/.

Your class must be like this:

public class Data
{
        public int temperature { get; set; }
        public string wind_direction { get; set; }
        public double wind_velocity { get; set; }
        public double humidity { get; set; }
        public string condition { get; set; }
        public double pressure { get; set; }
        public string icon { get; set; }
        public int sensation { get; set; }
        public string date { get; set; }
}
public class UsuarioCliente
{
        public int id { get; set; }
        public string name { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public Data data { get; set; }  
        public int IDUsuarioCliente { get; set; }
        public string Email_Cliente { get; set; }
        public string Login { get; set; }        
        public string SenhaCliente { get; set; }
        public string ConfirmacaoSenha { get; set; }
        public string CPF_Cliente { get; set; }
        public string Nome_Cliente { get; set; }  
        public string Telefone_Cliente { get; set; }
        public string Cep { get; set; }
        public string Logradouro { get; set; }
        public string Localidade { get; set; }
        public string Name { get; set; }
        public string Id { get; set; }
        public string State { get; set; }
 }

1


Looking at the documentation in http://apiadvisor.climatempo.com.br/doc/index.html, it is possible to see that your class does not have properties with the same name returned in json.

The basic class structure that meets the documentation and returned result would be like this:

public class Data
{
    public int temperature { get; set; }
    public string wind_direction { get; set; }
    public int wind_velocity { get; set; }
    public int humidity { get; set; }
    public string condition { get; set; }
    public int pressure { get; set; }
    public string icon { get; set; }
    public int sensation { get; set; }
    public string date { get; set; }
}

public class UsuarioCliente
{
    public int id { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public string country { get; set; }
    public Data data { get; set; }
}

I made a functional example to demonstrate the result here: dotnetfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.