I'm trying to find a value inside a JToken called "Contact_Name". The problem I am having is that it never enters the if statement, I have tried with other values and it never finds them.
RestResponse response = await client.GetAsync(request);
var results = JObject.Parse(response.Content);
var array = results["data"].Value<JArray>();
foreach (var result in array)
{
string _opportunity_description = result.Value<JObject>()["Description"].Value<String>();
string opportunity_id = result.Value<JObject>()["id"].Value<String>();
string opportunity_closing_date = result.Value<JObject>()["Closing_Date"].Value<String>();
string opportunity_closing_date_timestamp = DateTimeOffset.Parse(zoho_opportunity_closing_date).ToUnixTimeMilliseconds().ToString();
string opportunity_stage = result.Value<JObject>()["Stage"].Value<String>();
string opportunity_deal_name = result.Value<JObject>()["Deal_Name"].Value<String>();
string opportunity_probability = result.Value<JObject>()["Probability"].Value<String>();
string opportunity_account_id = result.Value<JObject>()["Account_Name"].Value<JObject>()["id"].Value<String>();
string opportunity_deal_owner_email = result.Value<JObject>()["Owner"].Value<JObject>()["email"].Value<String>();
if (result.Contains("Contact_Name"))
{
Console.WriteLine("kjlnas");
//string opportunity_deal_contact_id = opportunity_deal_contact_name.Value<JObject>()["id"].Value<String>();
} }
In postman i have found that Contact_Name can have two cases: First one:
"data": [
{
"Contact_Name" : null
}]
Second case:
"data": [
{
"Contact_Name" :{
"id": xxx,
"name": xxx}
}]
CodePudding user response:
try following way it may help I am not sure about it
If(!String.IsNullOrEmpty(result.Value<JObject>()["Contact_Name"].Value<String>()))
{
//Your Logic
}
CodePudding user response:
You can use something like this:
if (result.SelectToken("Contact_Name") != null)
{
// Process data
}