如何从JObject获取值

时间:2017-12-09 11:52:18

标签: c# json webhooks pipedrive-api

我想将Pipedrive与Dynamics CRM集成。只要在Pipedrive中更新交易,我就会调用webhook。我正在以JObject形式获取数据。我想从中提取两个字段。一个是身份,另一个是头衔。我该怎么做?

调用Webhook的代码:

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

我得到的Json:

{
  "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
}

3 个答案:

答案 0 :(得分:1)

如果它是常规的HTTP调用,您可以使用内置的JSON模型绑定器。 尝试将JObject更改为代表您的消息的类,例如: (结构已更新为提供JSON表示)

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

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

并在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}");            
    }
}

答案 1 :(得分:0)

你可以这样试试;

DeviceLocation deviceLoc;

答案 2 :(得分:0)

我会使用SelectToken。您可以为目标值指定JSONPath。假设您想要当前状态和标题,您可以这样做:

public abstract class CountryFactory {
  protected abstract CountryTypeHandler getCountryTypeHandler(String type);
}

public class USHandlerFactory{
  @Override
  protected USHandlerFactory getCountryTypeHandler(String type){
    if(type=="type1){
      return new USType1Handler();
    }
    else if(type=="type2"){
      return new USType2Handler();
    }
  }
}

public class UKHandlerFactory{
  @Override
  protected UKHandlerFactory getCountryTypeHandler(String type){
    if(type=="type1){
      return new UKType1Handler();
    }
    else if(type=="type2"){
      return new UKType2Handler();
    }
  }
}

public void YourExecutingClass {
  USHandlerFactory handler; // Since this is your handler interface, it can hold any type of handler. You don't need to instantiate all your handlers. 

  String country = "us"; // Both country and type are input and can vary anyway you handle
  String type = "type1"; 
  CountryFactory factory;  
  if(country=="us"){
    factory = new USHandlerFactory();
  } else if(country=="uk") {
    factory = new UKHandlerFactory();
  }else{
    throw exception("unknown country");
  }

  handler = factory.getCountryTypeHandler(type);
  handler.Execute();
}

如果您想要之前的状态和标题,请改用string status = (string)message.SelectToken("current.status"); string title = (string)message.SelectToken("current.title"); previous.status