3

Faço a um request a uma API, que me retorna dados em forma de JSON.

Exemplo:

{
"id": 8452,
"creator_user_id": {
"id": 46342,
"name": "ADM. Suporte Comercial",
"email": "*******@gmail.com",
"has_pic": true,
"pic_hash": "296e6514a7d0bf854fdcf22e60a19c9f",
"active_flag": false,
"value": 46342
},
"user_id": {
"id": 1875524,
"name": "09. Natália Grosso",
"email": "*******@gmail.com",
"has_pic": true,
"pic_hash": "e4f326a72bc341c84e7608993435c509",
"active_flag": true,
"value": 1875524
},
"person_id": {
"name": "Eng. Simone Biolo",
"email": [
{
"label": "work",
"value": "*******@gmail.com",
"primary": true
}
],
"phone": [
{
"label": "work",
"value": "***********",
"primary": true
}
],
"value": 10044
},
"org_id": null,
"stage_id": 45,
"title": "161366 - Lavatório Cirúrgico (cópia)",
"value": 25980,
"currency": "BRL",
"add_time": "2017-05-29 12:12:54",
"update_time": "2017-08-02 17:38:47",
"stage_change_time": "2017-07-24 11:52:56",
"active": true,
"deleted": false,
"status": "open",
"next_activity_date": "2017-08-16",
"next_activity_time": null,
"next_activity_id": 42869,
"last_activity_id": null,
"last_activity_date": null,
"lost_reason": null,
"visible_to": "3",
"close_time": null,
"pipeline_id": 6,
"won_time": null,
"first_won_time": null,
"lost_time": null,
"products_count": 0,
"files_count": 0,
"notes_count": 0,
"followers_count": 1,
"email_messages_count": 0,
"activities_count": 1,
"done_activities_count": 0,
"undone_activities_count": 1,
"reference_activities_count": 0,
"participants_count": 1,
"ac0ad7297e4688aec9d739f3bcc123d0e783b615": "53,25",
"4c67dc6df0ef9892524aae24f65c28dc11d3b765": "41",
"last_incoming_mail_time": null,
"last_outgoing_mail_time": null,
"stage_order_nr": 4,
"person_name": "Eng. Simone Biolo",
"org_name": null,
"next_activity_subject": "Instalação",
"next_activity_type": "call",
"next_activity_duration": null,
"next_activity_note": "Previsão de instalação em 15/08",
"formatted_value": "R$****",
"rotten_time": null,
"weighted_value": 0,
"formatted_weighted_value": "R$0",
"owner_name": "09. Natália Grosso",
"cc_email": "******@pipedrivemail.com",
"org_hidden": false,
"person_hidden": false
}

Então, copiei, e utilizei o colar especial do VS: Paste JSON as Class, que me gerou o Model abaixo:

public class Projetos
{
    [Key]
    public int id { get; set; }
    public Creator_User_Id creator_user_id { get; set; }
    public User_Id user_id { get; set; }
    public Person_Id person_id { get; set; }
    public object org_id { get; set; }
    public int stage_id { get; set; }
    public string title { get; set; }
    public int value { get; set; }
    public string currency { get; set; }
    public string add_time { get; set; }
    public string update_time { get; set; }
    public string stage_change_time { get; set; }
    public bool active { get; set; }
    public bool deleted { get; set; }
    public string status { get; set; }
    public string next_activity_date { get; set; }
    public object next_activity_time { get; set; }
    public int next_activity_id { get; set; }
    public object last_activity_id { get; set; }
    public object last_activity_date { get; set; }
    public object lost_reason { get; set; }
    public string visible_to { get; set; }
    public object close_time { get; set; }
    public int pipeline_id { get; set; }
    public object won_time { get; set; }
    public object first_won_time { get; set; }
    public object lost_time { get; set; }
    public int products_count { get; set; }
    public int files_count { get; set; }
    public int notes_count { get; set; }
    public int followers_count { get; set; }
    public int email_messages_count { get; set; }
    public int activities_count { get; set; }
    public int done_activities_count { get; set; }
    public int undone_activities_count { get; set; }
    public int reference_activities_count { get; set; }
    public int participants_count { get; set; }
    public object ac0ad7297e4688aec9d739f3bcc123d0e783b615 { get; set; }
    public string _4c67dc6df0ef9892524aae24f65c28dc11d3b765 { get; set; }
    public object last_incoming_mail_time { get; set; }
    public object last_outgoing_mail_time { get; set; }
    public int stage_order_nr { get; set; }
    public string person_name { get; set; }
    public object org_name { get; set; }
    public string next_activity_subject { get; set; }
    public string next_activity_type { get; set; }
    public object next_activity_duration { get; set; }
    public string next_activity_note { get; set; }
    public string formatted_value { get; set; }
    public object rotten_time { get; set; }
    public int weighted_value { get; set; }
    public string formatted_weighted_value { get; set; }
    public string owner_name { get; set; }
    public string cc_email { get; set; }
    public bool org_hidden { get; set; }
    public bool person_hidden { get; set; }
}

public class Creator_User_Id
{
    public int id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public bool has_pic { get; set; }
    public string pic_hash { get; set; }
    public bool active_flag { get; set; }
    public int value { get; set; }
}

public class User_Id
{
    public int id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public bool has_pic { get; set; }
    public string pic_hash { get; set; }
    public bool active_flag { get; set; }
    public int value { get; set; }
}

public class Person_Id
{
    public string name { get; set; }
    public Email[] email { get; set; }
    public Phone[] phone { get; set; }
    public int value { get; set; }
}

public class Email
{
    public string label { get; set; }
    public string value { get; set; }
    public bool primary { get; set; }
}

public class Phone
{
    public string label { get; set; }
    public string value { get; set; }
    public bool primary { get; set; }
}

Faço o Request via HttpClient:

  public class ProjetosController : Controller
{
    private static HttpClient httpClient;

    static ProjetosController()
    {
        httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri("https://*****.pipedrive.com/v1/");
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //Se precisar adicionar alguma autenticação, faça o aqui.        
    }

    [HttpGet]
    public async Task<ActionResult> Index()
    {           
        var queryString = $"deals?stage_id=45&status=open&start=0&api_token=************************22f3a2c";
        var response = await httpClient.GetAsync(queryString);
        if (response.IsSuccessStatusCode)
        {
            var json = await response.Content.ReadAsStringAsync();                
            var model = Newtonsoft.Json.JsonConvert.DeserializeObject<Projetos>(json);
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Erro ao acessar a API.");
    }
}

Porém, executar o DeserializeObject, tenho como retorno todos os objetos como null

inserir a descrição da imagem aqui

Retorno do var json = await response.Content.ReadAsStringAsync();

inserir a descrição da imagem aqui

Ao tentar rodar a solução do colaborador @LINQ, é gerado a exceção abaixo:

inserir a descrição da imagem aqui

4
  • seu var json = await ... ta retornando valores? 11/08/2017 às 19:29
  • Sim.. esta certinho.. retornando todos os valores 11/08/2017 às 19:29
  • Conseguiu resolver o problema, jovem?
    – Jéf Bueno
    14/08/2017 às 14:32
  • @linq so vou conseguir mexer nesse projeto, a noite.. mas hoje ja tenho um retorno. Obrigado 14/08/2017 às 15:57

2 Respostas 2

4

O problema é que o JSON não está no formato que você indicou.

Eu acabei de ver a documentação da API v1 do PipeDrive e nela diz que o formato da resposta para requisições ao recurso deals (usando o verbo GET), é assim:

{
    "success": true,
    "data": null,
    "additional_data": {
        "pagination": {
            "start": 0,
            "limit": 100,
            "more_items_in_collection": false
        }
    }
}

Ou seja, não há relação entre o model destino da serialização e o payload recebido da requisição.

O que eu te indicaria é criar um model para representar as respostas da API do PipeDrive.

Eu fiz genérico, porque é bem provável que a resposta seja assim para todos os recursos.

class PipeDriveResponse<T> where T : new()
{
    public bool Success { get; set; }
    public T Data { get; set; }
    public DadosAdicionais AdditionalData { get; set; }       
}

class DadosAdicionais
{
    public Paginacao { get; set; }
}

class Paginacao
{
    public int Start { get; set; }
    public int Limit { get; set; }
    public bool MoreItensInCollection { get; set; }
}

E, na desearialização, vai ser necessário trocar o tipo:

// resto do código
var model = JsonConvert.DeserializeObject<PipeDriveResponse<Projetos>>(json);
// resto do código

Disclaimer: Eu não sei como o deseralizador está configurado, então escrevi as classes com os nomes das propriedades em TitleCasing. Talvez, então, seja preciso adaptar o nome das propriedades. De qualquer forma, a ideia continua exatamente a mesma.

5
  • amigo boa tarde, ao tentar executar a sua solução é gerado um erro, fiz uma atualização mostrando obrigado 15/08/2017 às 19:49
  • O erro diz claramente que a resposta não é um JSON válido. Verifique a string de resposta.
    – Jéf Bueno
    15/08/2017 às 19:56
  • bom dia, dentro do campo data terei varios retornos, como pode ser visto no JSON Visualizer que postei na pergunta, pra esses casos preciso de uma tratativa diferente? como crio essa deseralização de vários itens? 16/08/2017 às 11:03
  • @ThomasErichPimentel Não, justamente por isso que eu fiz a classe que "envolve" os dados como genérica
    – Jéf Bueno
    16/08/2017 às 12:08
  • consegui resolver, acontece que, no primeiro registro data ele tras org_id como null.. sendo que ele é uma classe! então criei a classe e rodou certinho.. obrigado. 16/08/2017 às 12:10
1

Eu não tive problema para consumir seu JSON.

Montei uma POC GIT assim.

static void Main(string[] args)
{
    {
        string path = @"C:\work\stackoverflow\stackoverflow\ConsoleApp1\bin\Debug\new 1.txt";
        // Open the file to read from.
        string readText = File.ReadAllText(path);

        var model = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(readText);
    }
}

Só as classes que gerei por aqui.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class CreatorUserId
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public bool has_pic { get; set; }
        public string pic_hash { get; set; }
        public bool active_flag { get; set; }
        public int value { get; set; }
    }

    public class UserId
    {
        public int id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public bool has_pic { get; set; }
        public string pic_hash { get; set; }
        public bool active_flag { get; set; }
        public int value { get; set; }
    }

    public class Email
    {
        public string label { get; set; }
        public string value { get; set; }
        public bool primary { get; set; }
    }

    public class Phone
    {
        public string label { get; set; }
        public string value { get; set; }
        public bool primary { get; set; }
    }

    public class PersonId
    {
        public string name { get; set; }
        public List<Email> email { get; set; }
        public List<Phone> phone { get; set; }
        public int value { get; set; }
    }

    public class RootObject
    {
        public int id { get; set; }
        public CreatorUserId creator_user_id { get; set; }
        public UserId user_id { get; set; }
        public PersonId person_id { get; set; }
        public object org_id { get; set; }
        public int stage_id { get; set; }
        public string title { get; set; }
        public int value { get; set; }
        public string currency { get; set; }
        public string add_time { get; set; }
        public string update_time { get; set; }
        public string stage_change_time { get; set; }
        public bool active { get; set; }
        public bool deleted { get; set; }
        public string status { get; set; }
        public string next_activity_date { get; set; }
        public object next_activity_time { get; set; }
        public int next_activity_id { get; set; }
        public object last_activity_id { get; set; }
        public object last_activity_date { get; set; }
        public object lost_reason { get; set; }
        public string visible_to { get; set; }
        public object close_time { get; set; }
        public int pipeline_id { get; set; }
        public object won_time { get; set; }
        public object first_won_time { get; set; }
        public object lost_time { get; set; }
        public int products_count { get; set; }
        public int files_count { get; set; }
        public int notes_count { get; set; }
        public int followers_count { get; set; }
        public int email_messages_count { get; set; }
        public int activities_count { get; set; }
        public int done_activities_count { get; set; }
        public int undone_activities_count { get; set; }
        public int reference_activities_count { get; set; }
        public int participants_count { get; set; }
        public string ac0ad7297e4688aec9d739f3bcc123d0e783b615 { get; set; }
        public string __invalid_name__4c67dc6df0ef9892524aae24f65c28dc11d3b765 { get; set; }
        public object last_incoming_mail_time { get; set; }
        public object last_outgoing_mail_time { get; set; }
        public int stage_order_nr { get; set; }
        public string person_name { get; set; }
        public object org_name { get; set; }
        public string next_activity_subject { get; set; }
        public string next_activity_type { get; set; }
        public object next_activity_duration { get; set; }
        public string next_activity_note { get; set; }
        public string formatted_value { get; set; }
        public object rotten_time { get; set; }
        public int weighted_value { get; set; }
        public string formatted_weighted_value { get; set; }
        public string owner_name { get; set; }
        public string cc_email { get; set; }
        public bool org_hidden { get; set; }
        public bool person_hidden { get; set; }
    }
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .