CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
// https://stackoverflow.com/questions/47628090/need-to-grab-a-parent-value-in-linq
// https://stackoverflow.com/questions/47620760/using-data-from-deserialized-nested-json
public partial class Welcome
{
[JsonProperty("data")]
public Datum[] Data { get; set; }
[JsonProperty("has_more")]
public bool HasMore { get; set; }
[JsonProperty("total_results")]
public long TotalResults { get; set; }
}
public partial class Datum
{
[JsonProperty("addresses")]
public object[] Addresses { get; set; }
[JsonProperty("contacts")]
public Contact[] Contacts { get; set; }
[JsonProperty("created_by")]
public string CreatedBy { get; set; }
[JsonProperty("created_by_name")]
public string CreatedByName { get; set; }
[JsonProperty("custom")]
public Custom Custom { get; set; }
[JsonProperty("custom.lcf_dDtwGb41tpi8XkVMMgxdHxEJ92A7ujreRU1aPPZd95B")]
public string CustomInitialService { get; set; }
[JsonProperty("custom.lcf_fUNjcSq8bemwdQL0wogc3wgyxmz3ZD17fKdv8s4wkWV")]
public string CustomLeadOwner { get; set; }
[JsonProperty("custom.lcf_UfNWMxg2f7UU28rI9RAhTEAPWMyXU1UFZb5oNJEgR0K")]
public string CustomMarketingSource { get; set; }
[JsonProperty("custom.lcf_y37bo72LesrOBWuVPSoRfIazw5KflujnV81nGCfcjZ3")]
public string CustomDateCreated { get; set; }
[JsonProperty("date_created")]
public string DateCreated { get; set; }
[JsonProperty("date_updated")]
public string DateUpdated { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("display_name")]
public string DisplayName { get; set; }
[JsonProperty("html_url")]
public string HtmlUrl { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("integration_links")]
public IntegrationLink[] IntegrationLinks { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("opportunities")]
public Opportunity[] Opportunities { get; set; }
[JsonProperty("organization_id")]
public string OrganizationId { get; set; }
[JsonProperty("status_id")]
public string StatusId { get; set; }
[JsonProperty("status_label")]
public string StatusLabel { get; set; }
[JsonProperty("tasks")]
public object[] Tasks { get; set; }
[JsonProperty("updated_by")]
public string UpdatedBy { get; set; }
[JsonProperty("updated_by_name")]
public string UpdatedByName { get; set; }
[JsonProperty("url")]
public object Url { get; set; }
}
public partial class Opportunity
{
[JsonProperty("confidence")]
public long Confidence { get; set; }
[JsonProperty("contact_id")]
public object ContactId { get; set; }
[JsonProperty("contact_name")]
public object ContactName { get; set; }
[JsonProperty("created_by")]
public string CreatedBy { get; set; }
[JsonProperty("created_by_name")]
public string CreatedByName { get; set; }
[JsonProperty("date_created")]
public string DateCreated { get; set; }
[JsonProperty("date_lost")]
public object DateLost { get; set; }
[JsonProperty("date_updated")]
public string DateUpdated { get; set; }
[JsonProperty("date_won")]
public object DateWon { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("integration_links")]
public object[] IntegrationLinks { get; set; }
[JsonProperty("lead_id")]
public string LeadId { get; set; }
[JsonProperty("lead_name")]
public string LeadName { get; set; }
[JsonProperty("note")]
public string Note { get; set; }
[JsonProperty("organization_id")]
public string OrganizationId { get; set; }
[JsonProperty("status_id")]
public string StatusId { get; set; }
[JsonProperty("status_label")]
public string StatusLabel { get; set; }
[JsonProperty("status_type")]
public string StatusType { get; set; }
[JsonProperty("updated_by")]
public string UpdatedBy { get; set; }
[JsonProperty("updated_by_name")]
public string UpdatedByName { get; set; }
[JsonProperty("user_id")]
public string UserId { get; set; }
[JsonProperty("user_name")]
public string UserName { get; set; }
[JsonProperty("value")]
public long Value { get; set; }
[JsonProperty("value_currency")]
public string ValueCurrency { get; set; }
[JsonProperty("value_formatted")]
public string ValueFormatted { get; set; }
[JsonProperty("value_period")]
public string ValuePeriod { get; set; }
}
public partial class Custom
{
[JsonProperty("Date Created")]
public string DateCreated { get; set; }
[JsonProperty("Initial Service")]
public string InitialService { get; set; }
[JsonProperty("Lead Owner")]
public string LeadOwner { get; set; }
[JsonProperty("Marketing Source")]
public string MarketingSource { get; set; }
}
public partial class Contact
{
[JsonProperty("created_by")]
public string CreatedBy { get; set; }
[JsonProperty("date_created")]
public string DateCreated { get; set; }
[JsonProperty("date_updated")]
public string DateUpdated { get; set; }
[JsonProperty("emails")]
public Email[] Emails { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("integration_links")]
public IntegrationLink[] IntegrationLinks { get; set; }
[JsonProperty("lead_id")]
public string LeadId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("organization_id")]
public string OrganizationId { get; set; }
[JsonProperty("phones")]
public Phone[] Phones { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("updated_by")]
public string UpdatedBy { get; set; }
[JsonProperty("urls")]
public object[] Urls { get; set; }
}
public partial class Phone
{
[JsonProperty("phone_formatted")]
public string PhoneFormatted { get; set; }
[JsonProperty("phone")]
public string PurplePhone { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public class Email
{
[JsonProperty("email")]
public string EmailValue { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class IntegrationLink
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
}
public partial class Welcome
{
//public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}
public static class Serialize
{
//public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
class TestClass
{
public static void Test()
{
var html = GetJson();
var json = JsonConvert.DeserializeObject<Welcome>(html);
var query = from d in json.Data
from c in d.Contacts
from e in c.Emails
select new { Email = e.EmailValue, LeadId = c.LeadId, Id = d.Id };
var emails = query.ToList();
Console.WriteLine("Emails: {0}", JsonConvert.SerializeObject(emails));
//Console.WriteLine(JsonConvert.SerializeObject(json, Formatting.Indented));
}
static string GetJson()
{
var json = @"{
""data"": [
{
""addresses"": [],
""contacts"": [
{
""created_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""date_created"": ""2017-12-03T05:42:30.737000+00:00"",
""date_updated"": ""2017-12-03T05:45:32.227000+00:00"",
""emails"": [
{
""email"": ""[email protected]"",
""type"": ""office""
}
],
""id"": ""cont_kH9tcx970T3ZTGKPPP2vh5u3Ha8P9Rjz6gGqflgzwvy"",
""integration_links"": [
{
""name"": ""LinkedIn Search"",
""url"": ""https://www.linkedin.com/search/results/index/?keywords=Test%20Woodard""
}
],
""lead_id"": ""lead_12Rz7R2TrWmwUhCGVTRh8rt6A8hokaXgujSEPlfoWpD"",
""name"": ""Test User"",
""organization_id"": ""orga_iFnIMLDbvGevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH"",
""phones"": [
{
""phone"": ""+15558675309"",
""phone_formatted"": ""+1 555-867-5309"",
""type"": ""office""
}
],
""title"": """",
""updated_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""urls"": []
}
],
""created_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""created_by_name"": ""Test User"",
""custom"": {
""Date Created"": ""2017-12-03"",
""Initial Service"": ""Men's Health"",
""Lead Owner"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""Marketing Source"": ""Bing""
},
""custom.lcf_UfNWMxg2f7UU28rI9RAhTEAPWMyXU1UFZb5oNJEgR0K"": ""Bing"",
""custom.lcf_dDtwGb41tpi8XkVMMgxdHxEJ92A7ujreRU1aPPZd95B"": ""Chiropractor"",
""custom.lcf_fUNjcSq8bemwdQL0wogc3wgyxmz3ZD17fKdv8s4wkWV"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""custom.lcf_y37bo72LesrOBWuVPSoRfIazw5KflujnV81nGCfcjZ3"": ""2017-12-03"",
""date_created"": ""2017-12-03T05:42:30.733000+00:00"",
""date_updated"": ""2017-12-03T05:45:32.234000+00:00"",
""description"": """",
""display_name"": ""test"",
""html_url"": ""https://app.close.io/lead/lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpG/"",
""id"": ""lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpD"",
""integration_links"": [
{
""name"": ""Google Search"",
""url"": ""http://google.com/search?q=test""
}
],
""name"": ""test"",
""opportunities"": [
{
""confidence"": 50,
""contact_id"": null,
""contact_name"": null,
""created_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""created_by_name"": ""Test User"",
""date_created"": ""2017-12-03T05:44:31.131000+00:00"",
""date_lost"": null,
""date_updated"": ""2017-12-03T05:44:31.131000+00:00"",
""date_won"": null,
""id"": ""oppo_xQ5S1dHMjDWoeUBAigtyiwuwFUkxtbxHURQX5UaL7fT"",
""integration_links"": [],
""lead_id"": ""lead_12Rz7R2TrWmwUhCFVTRh8rt6A8hokaXgujSEPlfoWpD"",
""lead_name"": ""test"",
""note"": ""New Lead"",
""organization_id"": ""orga_iFnIMLDbvNevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH"",
""status_id"": ""stat_SwQmzqoIqIt20j9YBDxIYem11GyZPGL30G7PdkWYdvG"",
""status_label"": ""Chiropractor"",
""status_type"": ""active"",
""updated_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""updated_by_name"": ""Test User"",
""user_id"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""user_name"": ""Test User"",
""value"": 140000,
""value_currency"": ""USD"",
""value_formatted"": ""$1,400"",
""value_period"": ""one_time""
}
],
""organization_id"": ""orga_iFnIMLDbvNevDrXMHYQSbYnLu1GjrdM2TEWWsCoraJH"",
""status_id"": ""stat_auhCEJZNhaUnX9DHdrCaZ5r5mhpqxjfaE1hdwnPz6xx"",
""status_label"": ""Potential"",
""tasks"": [],
""updated_by"": ""user_C8ptFaOBRGnsYadWFUZCRCYXRnuV5Ch8Lgr9LKXz7zG"",
""updated_by_name"": ""Test User"",
""url"": null
}
],
""has_more"": false,
""total_results"": 1
}";
return json;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}
public class AssertionFailedException : System.Exception
{
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
}
public static class Assert
{
public static void IsTrue(bool value)
{
IsTrue(value, "failed");
}
public static void IsTrue(bool value, string message)
{
if (value == false)
throw new AssertionFailedException(message ?? "failed");
}
}