I am trying to parse a multilayer json from url and then the parsed json rootobject in datatables for insert and update command in c#.
By far its inserting values into database but what i need is that this below code should first get convert into datatable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication3.Models
{
public class Customer
{
}
public class Meta
{
public string type { get; set; }
public string date { get; set; }
public string schemaUrl { get; set; }
}
public class BillingContact
{
public string language { get; set; }
public string languageId { get; set; }
public string locale { get; set; }
public string company { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string companyRomanized { get; set; }
public string firstnameRomanized { get; set; }
public string lastnameRomanized { get; set; }
public string street1 { get; set; }
public string street2 { get; set; }
public string postalcode { get; set; }
public string city { get; set; }
public string state { get; set; }
public string stateId { get; set; }
public string stateIsoCode { get; set; }
public string country { get; set; }
public string countryId { get; set; }
public string email { get; set; }
}
public class DeliveryContact
{
public string language { get; set; }
public string languageId { get; set; }
public string locale { get; set; }
public string company { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string companyRomanized { get; set; }
public string firstnameRomanized { get; set; }
public string lastnameRomanized { get; set; }
public string street1 { get; set; }
public string street2 { get; set; }
public string postalcode { get; set; }
public string city { get; set; }
public string state { get; set; }
public string stateId { get; set; }
public string stateIsoCode { get; set; }
public string country { get; set; }
public string countryId { get; set; }
public string email { get; set; }
}
public class LicenseeContact
{
public string language { get; set; }
public string languageId { get; set; }
public string locale { get; set; }
public string company { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string companyRomanized { get; set; }
public string firstnameRomanized { get; set; }
public string lastnameRomanized { get; set; }
public string street1 { get; set; }
public string street2 { get; set; }
public string postalcode { get; set; }
public string city { get; set; }
public string state { get; set; }
public string stateId { get; set; }
public string stateIsoCode { get; set; }
public string country { get; set; }
public string countryId { get; set; }
public string email { get; set; }
}
public class PaymentInfo
{
public string currency { get; set; }
public string currencyId { get; set; }
public string paymentType { get; set; }
public string paymentTypeId { get; set; }
public bool isPurchaseOrder { get; set; }
}
public class ProfitCalculation
{
public double grossRevenue { get; set; }
public double collectedVat { get; set; }
public double netRevenue { get; set; }
public double cbMarginPercentage { get; set; }
public double cbMarginFix { get; set; }
public double yourNetProfit { get; set; }
public double yourVat { get; set; }
public double yourGrossProfit { get; set; }
}
public class ProductSinglePrice
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class ProductTotalPrice
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class TotalSinglePrice
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class TotalTotalPrice
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class YourPrice
{
public ProductSinglePrice productSinglePrice { get; set; }
public ProductTotalPrice productTotalPrice { get; set; }
public TotalSinglePrice totalSinglePrice { get; set; }
public TotalTotalPrice totalTotalPrice { get; set; }
}
public class ProductSinglePrice2
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class ProductTotalPrice2
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class TotalSinglePrice2
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class TotalTotalPrice2
{
public double netPrice { get; set; }
public double vatPrice { get; set; }
public double grossPrice { get; set; }
public double vatPercentage { get; set; }
}
public class CustomerPrice
{
public ProductSinglePrice2 productSinglePrice { get; set; }
public ProductTotalPrice2 productTotalPrice { get; set; }
public TotalSinglePrice2 totalSinglePrice { get; set; }
public TotalTotalPrice2 totalTotalPrice { get; set; }
}
public class Service
{
public string deliveryType { get; set; }
public string deliveryTypeId { get; set; }
}
public class Delivery
{
public Service service { get; set; }
}
public class NextBillingProfit
{
public double grossRevenue { get; set; }
public double collectedVat { get; set; }
public double netRevenue { get; set; }
public double cbMarginPercentage { get; set; }
public double cbMarginFix { get; set; }
public double yourNetProfit { get; set; }
public double yourVat { get; set; }
public double yourGrossProfit { get; set; }
}
public class RecurringBilling
{
public string subscriptionId { get; set; }
public string subscriptionEventtypeId { get; set; }
public int originalPurchaseId { get; set; }
public int originalPurchaseItemRunningNumber { get; set; }
public int intervalNumber { get; set; }
public int gracePeriodDays { get; set; }
public string statusId { get; set; }
public string status { get; set; }
public string itemStatusId { get; set; }
public string itemStatus { get; set; }
public string nextBillingDate { get; set; }
public string cancellationUrl { get; set; }
public string changePaymentSubscriptionUrl { get; set; }
public NextBillingProfit nextBillingProfit { get; set; }
}
public class Item
{
public int runningNumber { get; set; }
public int productId { get; set; }
public string productName { get; set; }
public string yourProductId { get; set; }
public string yourProductName { get; set; }
public string internalCategory { get; set; }
public int supportContactId { get; set; }
public string supportContact { get; set; }
public int clientId { get; set; }
public int quantity { get; set; }
public string yourCurrency { get; set; }
public string yourCurrencyId { get; set; }
public ProfitCalculation profitCalculation { get; set; }
public YourPrice yourPrice { get; set; }
public CustomerPrice customerPrice { get; set; }
public List<Delivery> deliveries { get; set; }
public RecurringBilling recurringBilling { get; set; }
}
public class ExtraParameters
{
}
public class RootObject
{
public Meta meta { get; set; }
public int purchaseId { get; set; }
public string status { get; set; }
public string statusId { get; set; }
public BillingContact billingContact { get; set; }
public DeliveryContact deliveryContact { get; set; }
public LicenseeContact licenseeContact { get; set; }
public PaymentInfo paymentInfo { get; set; }
public string creationTime { get; set; }
public string paymentArriveTime { get; set; }
public string lastModificationTime { get; set; }
public string subscriptionRevenueCategoryId { get; set; }
public string remoteAddress { get; set; }
public string remoteHost { get; set; }
public string httpUserAgent { get; set; }
public string httpEntryUrl { get; set; }
public string httpReferer { get; set; }
public string httpAcceptLanguage { get; set; }
public string internalCustomer { get; set; }
public string customerConfirmationPageUrl { get; set; }
public string customerPdfDocumentUrl { get; set; }
public string merchantOfRecord { get; set; }
public List<Item> items { get; set; }
public ExtraParameters extraParameters { get; set; }
}
}
<pre>I done it like this...its insering values in database...but not through datatable...can anyone share some code or procedure?
<pre> public void customerjson()
{
string url = @"https://api.myjson.com/bins/yxi3x";
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
var table = JsonConvert.DeserializeObject<RootObject>(json);
string sql = "";
SqlConnection _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb"].ToString());
sql = sql + "insert into ContactDetails (language, languageId, locale, company, firstname, lastname, companyRomanized, firstnameRomanized, lastnameRomanized, street1, street2, postalcode, city, state, stateId, stateIsoCode, country, countryId, email) values('"
+ table.billingContact.language.ToString() + "','"
+ table.billingContact.languageId.ToString() + "','"
+ table.billingContact.locale.ToString() + "','"
+ table.billingContact.company.ToString() + "','"
+ table.billingContact.firstname.ToString() + "','"
+ table.billingContact.lastname.ToString() + "','"
+ table.billingContact.companyRomanized.ToString() + "','"
+ table.billingContact.firstnameRomanized.ToString() + "','"
+ table.billingContact.lastnameRomanized.ToString() + "','"
+ table.billingContact.street1.ToString() + "','"
+ table.billingContact.street2.ToString() + "','"
+ table.billingContact.postalcode.ToString() + "','"
+ table.billingContact.city.ToString() + "','"
+ table.billingContact.state.ToString() + "','"
+ table.billingContact.stateId.ToString() + "','"
+ table.billingContact.stateIsoCode.ToString() + "','"
+ table.billingContact.country.ToString() + "','"
+ table.billingContact.countryId.ToString() + "','"
+ table.billingContact.email.ToString() + "')";
SqlCommand cmd = new SqlCommand(sql, _connection);
_connection.Open();
cmd.ExecuteNonQuery();
_connection.Close();
}
}