简体   繁体   中英

How to get values from JObject

I want to integrate Pipedrive with Dynamics CRM. I am calling webhook whenever deal is updated in Pipedrive. I am getting data in JObject form. I want to extract two fields from that. One is status and other is title.How can I do that?

Code to call Webhook:

 [RoutePrefix("api/webhook")]
public class WebhookController : ApiController
{
    [HttpPost]
    [Route("")]
    public void Post(JObject message)
    {
        Console.WriteLine($"Received webhook: {message}");            
    }
}

Json which I am getting :

{
  "v":1,
  "matches_filters":{
    "current":[

    ]
  },
  "meta":{
    "v":1,
    "action":"updated",
    "object":"deal",
    "id":1,
    "company_id":2278508,
    "user_id":3371496,
    "host":"agniket.pipedrive.com",
    "timestamp":1512620040,
    "timestamp_micro":1512620040475200,
    "permitted_user_ids":[
      3371496
    ],
    "trans_pending":false,
    "is_bulk_update":false,
    "pipedrive_service_name":false,
    "matches_filters":{
      "current":[

      ]
    },
    "webhook_id":"31814"
  },
  "current":{
    "id":1,
    "public_id":null,
    "creator_user_id":3371496,
    "user_id":3371496,
    "person_id":1,
    "org_id":null,
    "stage_id":1,
    "title":"vaishali deal",
    "value":0,
    "currency":"INR",
    "add_time":"2017-12-04 04:44:15",
    "update_time":"2017-12-07 04:14:00",
    "stage_change_time":null,
    "active":false,
    "deleted":false,
    "status":"won",
    "probability":null,
    "next_activity_date":null,
    "next_activity_time":null,
    "next_activity_id":null,
    "last_activity_id":null,
    "last_activity_date":null,
    "lost_reason":null,
    "visible_to":"3",
    "close_time":"2017-12-07 04:14:00",
    "pipeline_id":1,
    "won_time":"2017-12-07 04:14:00",
    "first_won_time":"2017-12-07 04:14:00",
    "lost_time":null,
    "products_count":0,
    "files_count":0,
    "notes_count":0,
    "followers_count":1,
    "email_messages_count":0,
    "activities_count":0,
    "done_activities_count":0,
    "undone_activities_count":0,
    "reference_activities_count":0,
    "participants_count":1,
    "expected_close_date":null,
    "last_incoming_mail_time":null,
    "last_outgoing_mail_time":null,
    "stage_order_nr":1,
    "person_name":"vaishali",
    "org_name":null,
    "next_activity_subject":null,
    "next_activity_type":null,
    "next_activity_duration":null,
    "next_activity_note":null,
    "formatted_value":"₹0",
    "rotten_time":null,
    "weighted_value":0,
    "formatted_weighted_value":"₹0",
    "owner_name":"riya dalvi",
    "cc_email":"agniket+deal1@pipedrivemail.com",
    "org_hidden":false,
    "person_hidden":false
  },
  "previous":{
    "id":1,
    "public_id":null,
    "creator_user_id":3371496,
    "user_id":3371496,
    "person_id":1,
    "org_id":null,
    "stage_id":1,
    "title":"vaishali deal",
    "value":0,
    "currency":"INR",
    "add_time":"2017-12-04 04:44:15",
    "update_time":"2017-12-04 06:33:56",
    "stage_change_time":null,
    "active":true,
    "deleted":false,
    "status":"open",
    "probability":null,
    "next_activity_date":null,
    "next_activity_time":null,
    "next_activity_id":null,
    "last_activity_id":null,
    "last_activity_date":null,
    "lost_reason":null,
    "visible_to":"3",
    "close_time":null,
    "pipeline_id":1,
    "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":0,
    "done_activities_count":0,
    "undone_activities_count":0,
    "reference_activities_count":0,
    "participants_count":1,
    "expected_close_date":null,
    "last_incoming_mail_time":null,
    "last_outgoing_mail_time":null,
    "stage_order_nr":1,
    "person_name":"vaishali",
    "org_name":null,
    "next_activity_subject":null,
    "next_activity_type":null,
    "next_activity_duration":null,
    "next_activity_note":null,
    "formatted_value":"₹0",
    "rotten_time":null,
    "weighted_value":0,
    "formatted_weighted_value":"₹0",
    "owner_name":"riya dalvi",
    "cc_email":"agniket+deal1@pipedrivemail.com",
    "org_hidden":false,
    "person_hidden":false
  },
  "event":"updated.deal",
  "retry":0
}

If it's a regular HTTP call you can just use built-in JSON model binder. Try to change your JObject to a class which represents your message, like: (Structure updated to provided JSON representation)

public class Message 
{
    public Current Current { get; set; }
}   

public class Current 
{
    public string Status { get; set; }
    public string Title { get; set; }
}

and use it in your WebHook :

[RoutePrefix("api/webhook")]
public class WebhookController : ApiController
{
    [HttpPost]
    [Route("")]
    public void Post(Message message)
    {
        Console.WriteLine($"Received webhook: {message.Current.Title} {message.Current.Status}");            
    }
}

You can try like this;

[HttpPost]
[Route("")]
public void Post(JObject message)
{
    var dynamicMessage = (dynamic)message;
    var status = dynamicMessage.status;
    var title = dynamicMessage.title;            
}

I would use SelectToken . You can specify a JSONPath to the target value. Assuming you want the current status and title you would do:

string status = (string)message.SelectToken("current.status");
string title = (string)message.SelectToken("current.title");

If you want the previous status and title, use previous.status and previous.title instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM