I'm not able to get the value from OneDay.price_change. The HTTP response is OK and I'm getting the following:
HTTP Response
"[{\"id\":\"BTC\",\"currency\":\"BTC\",\"symbol\":\"BTC\",\"name\":\"Bitcoin\",\"logo_url\":\"https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/btc.svg\",\"status\":\"active\",\"price\":\"60947.08258854\",\"price_date\":\"2021-10-31T00:00:00Z\",\"price_timestamp\":\"2021-10-31T18:51:00Z\",\"circulating_supply\":\"18860037\",\"max_supply\":\"21000000\",\"market_cap\":\"1149464232662\",\"market_cap_dominance\":\"0.4078\",\"num_exchanges\":\"397\",\"num_pairs\":\"67587\",\"num_pairs_unmapped\":\"5196\",\"first_candle\":\"2011-08-18T00:00:00Z\",\"first_trade\":\"2011-08-18T00:00:00Z\",\"first_order_book\":\"2017-01-06T00:00:00Z\",\"rank\":\"1\",\"high\":\"66082.82561618\",\"high_timestamp\":\"2021-10-20T00:00:00Z\",\"1h\":{\"volume\":\"1248590564.91\",\"price_change\":\"-85.32656234\",\"price_change_pct\":\"-0.0014\",\"volume_change\":\"-218879322.04\",\"volume_change_pct\":\"-0.1492\",\"market_cap_change\":\"-1607003923.65\",\"market_cap_change_pct\":\"-0.0014\"},\"1d\":{\"volume\":\"39937857069.60\",\"price_change\":\"-845.68642611\",\"price_change_pct\":\"-0.0137\",\"volume_change\":\"1918883279.43\",\"volume_change_pct\":\"0.0505\",\"market_cap_change\":\"-15892518975.54\",\"market_cap_change_pct\":\"-0.0136\"}}]\n"
However, for some reason, I'm not able to take the 1d price change. I'm not sure what could be the problem. Any help is appreciated!
Model:
public class OneHour
{
public string Volume { get; set; }
public string Price_change { get; set; }
public string Price_change_pct { get; set; }
public string Volume_change { get; set; }
public string Volume_change_pct { get; set; }
public string Market_cap_change { get; set; }
public string Market_cap_change_pct { get; set; }
}
public class OneDay
{
public string Volume { get; set; }
public string Price_change { get; set; }
public string Price_change_pct { get; set; }
public string Volume_change { get; set; }
public string Volume_change_pct { get; set; }
public string Market_cap_change { get; set; }
public string Market_cap_change_pct { get; set; }
}
public class CryptoApiMain
{
public OneHour OneHour { get; set; }
public OneDay OneDay { get; set; }
public string Id { get; set; }
public string Symbol { get; set; }
public string Status { get; set; }
public double Price { get; set; }
public string Price_date { get; set; }
public string Circulating_supply { get; set; }
public string Num_exchanges { get; set; }
public string Num_pairs { get; set; }
public string Rank { get; set; }
public string High { get; set; }
}
var theresponse = settingsService.CryptoApiResult(cryptoStock).Result;
foreach (var rez in theresponse)
{
<span id="stockSymbolCrypto">#cryptoStock</span>
<p>$#Convert.ToInt64(#rez.Price) #rez.OneDay.Price_change</p>
}
#rez.OneDay.Price_change error popup
Problem is that your property name in json (1d) and property name in c# model (OneDay) is not matching.
Use the below if you are using System.Text.Json (.Net Core 3.0 and newer)
[JsonPropertyName("1d")]
public OneDay OneDay { get; set; }
Use the below if you are using Newtonsoft (Before .Net Core 3.0)
[JsonProperty(PropertyName = "1d")]
public OneDay OneDay { get; set; }
Your " public OneHour OneHour { get; set; } " and " public OneDay OneDay { get; set; } " properties should be bind to [JsonProperty("1h")] and [JsonProperty("1d")]
try this
CryptoApiMain[] jsond = JsonConvert.DeserializeObject<CryptoApiMain[]>(json);
var price = jsond[0].OneDay.PriceChange;
result
-845.68642611
classes
public partial class CryptoApiMain
{
[JsonProperty("1h")]
public One OneHour { get; set; }
[JsonProperty("1d")]
public One OneDay { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("currency")]
public string Currency { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("logo_url")]
public Uri LogoUrl { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("price_date")]
public DateTimeOffset PriceDate { get; set; }
[JsonProperty("price_timestamp")]
public DateTimeOffset PriceTimestamp { get; set; }
[JsonProperty("circulating_supply")]
public long CirculatingSupply { get; set; }
[JsonProperty("max_supply")]
public long MaxSupply { get; set; }
[JsonProperty("market_cap")]
public string MarketCap { get; set; }
[JsonProperty("market_cap_dominance")]
public string MarketCapDominance { get; set; }
[JsonProperty("num_exchanges")]
public long NumExchanges { get; set; }
[JsonProperty("num_pairs")]
public long NumPairs { get; set; }
[JsonProperty("num_pairs_unmapped")]
public long NumPairsUnmapped { get; set; }
[JsonProperty("first_candle")]
public DateTimeOffset FirstCandle { get; set; }
[JsonProperty("first_trade")]
public DateTimeOffset FirstTrade { get; set; }
[JsonProperty("first_order_book")]
public DateTimeOffset FirstOrderBook { get; set; }
[JsonProperty("rank")]
public long Rank { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("high_timestamp")]
public DateTimeOffset HighTimestamp { get; set; }
}
public partial class One
{
[JsonProperty("volume")]
public string Volume { get; set; }
[JsonProperty("price_change")]
public string PriceChange { get; set; }
[JsonProperty("price_change_pct")]
public string PriceChangePct { get; set; }
[JsonProperty("volume_change")]
public string VolumeChange { get; set; }
[JsonProperty("volume_change_pct")]
public string VolumeChangePct { get; set; }
[JsonProperty("market_cap_change")]
public string MarketCapChange { get; set; }
[JsonProperty("market_cap_change_pct")]
public string MarketCapChangePct { get; set; }
}
Related
I know that this question is asked many times but still I'm struggling to understand this. I have below json to c# converted class.
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Accessibility
{
public bool share { get; set; }
public bool comment { get; set; }
}
public class Avatar
{
public string #base { get; set; }
}
public class Beautifulnari
{
public List<Post> posts { get; set; }
public long totalViewsCount { get; set; }
}
public class CaptionSignals
{
public bool safe { get; set; }
public Unsafe #unsafe { get; set; }
}
public class Category
{
public string id { get; set; }
public string name { get; set; }
}
public class Count
{
public int likes { get; set; }
public int views { get; set; }
}
public class Datum
{
public Haythandi haythandi { get; set; }
public Beautifulnari beautifulnari { get; set; }
}
public class HashtagDatum
{
public string _id { get; set; }
public string hashTagId { get; set; }
public string hashtagName { get; set; }
}
public class Haythandi
{
public List<Post> posts { get; set; }
public int totalViewsCount { get; set; }
}
public class Loc
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class MediaLocation
{
public int isHLS { get; set; }
public bool isTranscoded { get; set; }
public double duration { get; set; }
public string #base { get; set; }
public string thumbNailPath { get; set; }
public string path { get; set; }
public int mediaType { get; set; }
public string f0 { get; set; }
public string thumbnail { get; set; }
public Transcoded transcoded { get; set; }
public string compressedThumbnailPath { get; set; }
public string oPath { get; set; }
public string resizedThumbnailPath { get; set; }
public Resolution resolution { get; set; }
public Thumbnails thumbnails { get; set; }
public string webPath { get; set; }
}
public class ModerationStatus
{
public int approval { get; set; }
public int payment { get; set; }
public bool isModerated { get; set; }
public string moderatedBy { get; set; }
public string approvedBy { get; set; }
public DateTime? approvalDate { get; set; }
public DateTime? moderationDate { get; set; }
public int isAccepted { get; set; }
public string acceptedBy { get; set; }
public DateTime acceptanceDate { get; set; }
public object hashtagRejectReason { get; set; }
public int isOriginalAudio { get; set; }
public object ogAudioAcceptedBy { get; set; }
public object ogAudioAcceptanceDate { get; set; }
public object ogAudioRejectReason { get; set; }
public string acceptedHashtagId { get; set; }
}
public class ModerationV2Array
{
public string moderatorId { get; set; }
public string moderatorName { get; set; }
public object moderationSignals { get; set; }
public string response { get; set; }
}
public class OwnerData
{
public Avatar avatar { get; set; }
public string _id { get; set; }
public string name { get; set; }
public string username { get; set; }
public string status { get; set; }
public string profilePic { get; set; }
public int isProfileVerified { get; set; }
public int isFollowed { get; set; }
public int tipEnabled { get; set; }
}
public class Post
{
public string _id { get; set; }
public MediaLocation mediaLocation { get; set; }
public TempMedia tempMedia { get; set; }
public Song song { get; set; }
public Count count { get; set; }
public Accessibility accessibility { get; set; }
public OwnerData ownerData { get; set; }
public ModerationStatus moderationStatus { get; set; }
public TaggedStatus taggedStatus { get; set; }
public List<string> etags { get; set; }
public bool isDuplicate { get; set; }
public bool isProcessed { get; set; }
public string caption { get; set; }
public List<string> hashtagIds { get; set; }
public string youtubeLink { get; set; }
public bool isPrivate { get; set; }
public int reportPostsCount { get; set; }
public string audioLang { get; set; }
public object cta_text { get; set; }
public string ip { get; set; }
public string country { get; set; }
public string city { get; set; }
public string versionCode { get; set; }
public string createdBy { get; set; }
public string postType { get; set; }
public string uploadSource { get; set; }
public bool isSpam { get; set; }
public bool isPremium { get; set; }
public int metaProcessed { get; set; }
public int isShoppable { get; set; }
public int isLiked { get; set; }
public bool onlyFollowers { get; set; }
public bool isPrivateByAdmin { get; set; }
public int isPinned { get; set; }
public int isPromoted { get; set; }
public int isMiningAllowed { get; set; }
public string s3RefId { get; set; }
public string userId { get; set; }
public object duplicateOfPostId { get; set; }
public List<HashtagDatum> hashtagData { get; set; }
public List<Category> categories { get; set; }
public string gender { get; set; }
public int shareCount { get; set; }
public int likeCount { get; set; }
public int commentCount { get; set; }
public int viewsCount { get; set; }
public string status { get; set; }
public string language { get; set; }
public string created_at { get; set; }
public List<object> spamReason { get; set; }
public List<object> taggedUsers { get; set; }
public int __v { get; set; }
public Loc loc { get; set; }
public string region { get; set; }
public bool? enableV2 { get; set; }
public List<ModerationV2Array> moderationV2Array { get; set; }
public bool? ignoreV2 { get; set; }
public bool? isPremiumV2 { get; set; }
public bool? isSpamV2 { get; set; }
public List<PremiumCCSignal> premiumCCSignals { get; set; }
}
public class PremiumCCSignal
{
public string moderatorId { get; set; }
public CaptionSignals captionSignals { get; set; }
}
public class Resolution
{
public int width { get; set; }
public int height { get; set; }
}
public class Root
{
public int code { get; set; }
public List<Datum> data { get; set; }
public string message { get; set; }
public bool hasmoreData { get; set; }
public object error { get; set; }
}
public class Song
{
public string _id { get; set; }
public string title { get; set; }
public string art { get; set; }
public string author { get; set; }
public string categoryName { get; set; }
public object albumName { get; set; }
public int startTime { get; set; }
public double endTime { get; set; }
public string acrId { get; set; }
}
public class TaggedStatus
{
public bool isTagged { get; set; }
public string taggedBy { get; set; }
public DateTime taggedDate { get; set; }
}
public class TempMedia
{
public int isHls { get; set; }
}
public class Thumbnails
{
public string w50 { get; set; }
public string w150 { get; set; }
public string w300 { get; set; }
public string w700 { get; set; }
}
public class Transcoded
{
public string p1024 { get; set; }
public string p480 { get; set; }
public string p720 { get; set; }
}
public class Unsafe
{
}
In this above JSON, classes mentioned below are dynamic keys into response JSON.
Haythandi
Beautifulnari
The problem is that values Haythandi and beautifulNari as class name (if convert to c#) which are actually ids of that particular record. How do I convert these id's into c# class?, I have tried multiple approaches like using Dictionaries, JObjects, dynamic and Expando classes but not result. Any help would be much appreciated.
I think you need change one of the 'character classes' to:
public class MyClassWithPosts // was Beautifulnari
{
public List<Post> posts { get; set; }
public long totalViewsCount { get; set; }
}
After that data can become a dictionary (update: a list of dictionaries):
public class Root
{
public int code { get; set; }
public List<Dictionary<string, MyClassWithPosts>> data { get; set; }
Hi I am trying to deserialize this json. And my application does not do it for me.
i am using c#
any suggestion? thanks
this way i try to deserialize
var deserialize = resultado.Content.ReadAsStringAsync().Result;
var a = JsonConvert.DeserializeObject<product>(deserialize);
json received
{"product":{"id":6979552313549,"title":"Balance 100% Whey Protein 2.8kg w\/ FREE Magnesium complete powder","body_html":"Mountaineering backpack","vendor":"Balance","product_type":"physical","created_at":"2022-05-16T17:41:57-06:00","handle":"balance-100-whey-protein-2-8kg-w-free-magnesium-complete-powder-1","updated_at":"2022-05-26T12:34:07-06:00","published_at":"2022-05-16T17:41:57-06:00","template_suffix":null,"status":"active","published_scope":"web","tags":"Protein Powders, Specials, Stacks and Packs, Whey Protein Blend (WPI\/WPC)","admin_graphql_api_id":"gid:\/\/shopify\/Product\/6979552313549","variants":[{"id":40875072585933,"product_id":6979552313549,"title":"Default Title","price":"700.00","sku":"","position":1,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":"shopify","option1":"Default Title","option2":null,"option3":null,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","taxable":true,"barcode":null,"grams":0,"image_id":null,"weight":0.0,"weight_unit":"kg","inventory_item_id":42969806831821,"inventory_quantity":0,"old_inventory_quantity":0,"requires_shipping":true,"admin_graphql_api_id":"gid:\/\/shopify\/ProductVariant\/40875072585933"}],"options":[{"id":8937193341133,"product_id":6979552313549,"name":"Title","position":1,"values":["Default Title"]}],"images":[{"id":30230589407437,"product_id":6979552313549,"position":1,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","alt":null,"width":2862,"height":2143,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0618\/4189\/9725\/products\/Definici_C3_B3n-del-producto-y-servicio_0bf23268-fee3-4b3b-a577-aeaa2336d6fc.png?v=1653590047","variant_ids":[],"admin_graphql_api_id":"gid:\/\/shopify\/ProductImage\/30230589407437"}],"image":{"id":30230589407437,"product_id":6979552313549,"position":1,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","alt":null,"width":2862,"height":2143,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0618\/4189\/9725\/products\/Definici_C3_B3n-del-producto-y-servicio_0bf23268-fee3-4b3b-a577-aeaa2336d6fc.png?v=1653590047","variant_ids":[],"admin_graphql_api_id":"gid:\/\/shopify\/ProductImage\/30230589407437"}}}
If I have a Model class called "Product"
public class product
{
public long id { get; set; }
public string title { get; set; }
public string body_html { get; set; }
public string vendor { get; set; }
public string product_type { get; set; }
public DateTime created_at { get; set; }
public string handle { get; set; }
public DateTime updated_at { get; set; }
public DateTime published_at { get; set; }
public object template_suffix { get; set; }
public string status { get; set; }
public string published_scope { get; set; }
public string tags { get; set; }
public string admin_graphql_api_id { get; set; }
public List<Models.Producto.Variant> variants { get; set; }
public List<Models.Producto.Option> options { get; set; }
public List<Models.Producto.Images> images { get; set; }
public Models.Producto.Image image { get; set; }
}
I can see that you are using a wrong class to deserialize json, should be
var data=JsonConvert.DeserializeObject<Data>(json);
main classes (I highly recommend you to use a Pascal style for property names)
public partial class Data
{
[JsonProperty("product")]
public Product Product { get; set; }
}
public partial class Product
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("body_html")]
public string BodyHtml { get; set; }
[JsonProperty("vendor")]
public string Vendor { get; set; }
[JsonProperty("product_type")]
public string ProductType { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("handle")]
public string Handle { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("published_at")]
public DateTimeOffset PublishedAt { get; set; }
[JsonProperty("template_suffix")]
public object TemplateSuffix { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("published_scope")]
public string PublishedScope { get; set; }
[JsonProperty("tags")]
public string Tags { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
[JsonProperty("variants")]
public List<Variant> Variants { get; set; }
[JsonProperty("options")]
public List<Option> Options { get; set; }
[JsonProperty("images")]
public List<Image> Images { get; set; }
[JsonProperty("image")]
public Image Image { get; set; }
}
other classes
public partial class Image
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("alt")]
public object Alt { get; set; }
[JsonProperty("width")]
public long Width { get; set; }
[JsonProperty("height")]
public long Height { get; set; }
[JsonProperty("src")]
public Uri Src { get; set; }
[JsonProperty("variant_ids")]
public List<object> VariantIds { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
}
public partial class Option
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("values")]
public List<string> Values { get; set; }
}
public partial class Variant
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("inventory_policy")]
public string InventoryPolicy { get; set; }
[JsonProperty("compare_at_price")]
public object CompareAtPrice { get; set; }
[JsonProperty("fulfillment_service")]
public string FulfillmentService { get; set; }
[JsonProperty("inventory_management")]
public string InventoryManagement { get; set; }
[JsonProperty("option1")]
public string Option1 { get; set; }
[JsonProperty("option2")]
public object Option2 { get; set; }
[JsonProperty("option3")]
public object Option3 { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("taxable")]
public bool Taxable { get; set; }
[JsonProperty("barcode")]
public object Barcode { get; set; }
[JsonProperty("grams")]
public long Grams { get; set; }
[JsonProperty("image_id")]
public object ImageId { get; set; }
[JsonProperty("weight")]
public long Weight { get; set; }
[JsonProperty("weight_unit")]
public string WeightUnit { get; set; }
[JsonProperty("inventory_item_id")]
public long InventoryItemId { get; set; }
[JsonProperty("inventory_quantity")]
public long InventoryQuantity { get; set; }
[JsonProperty("old_inventory_quantity")]
public long OldInventoryQuantity { get; set; }
[JsonProperty("requires_shipping")]
public bool RequiresShipping { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
}
try this:
var response = JsonConvert.DeserializeObject<Dictionary<<string, string>>(deserialize);
string value = response["key"];
This is my model class.
In RootObject class I have a list of Trans class.In .net core when I am passing values to RootObject through Postman,I am getting null in RootObject.
public class Trans
{
public string SubDetailID { get; set; }
public string TrnType { get; set; }
public decimal amount { get; set; }
public string BankCurrencyRate { get; set; }
public decimal AccountID { get; set; }
public string Currency { set; get; }
public string CurrencyRate { set; get; }
public decimal JVID { get; set; }
public string coa { get; set; }
public decimal BusinessID { get; set; }
}
public class RootObject
{
public int JVID { get; set; }
public string InvoiceRef { get; set; }
public DateTime JVDate { get; set; }
public string StoreID { get; set; }
public decimal BusinessID { get; set; }
public string UserID { get; set; }
public string Currency { get; set; }
public string Rate { get; set; }
public DateTime DueDate { get; set; }
public string Reference { get; set; } = "Client";
public decimal RefID { get; set; }
public string Narration { get; set; }
public string Type { get; set; } = "CA";
public virtual List<Trans> transaction { get; set; }
}
I have a XML from that i want to get all node value. But after Deserialize i am getting null in my order node and inner node, can you please tell me what mistake I am doing.
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<Order Notes="Test Order" PackageTypeID="0" Service="Quote" CallerPhone="" CustomerCode="GA" Caller="" CalledBy="" CheckPiecesWeight="Y" ForceReRateFlag="True" RouteNumber="" OrderDate="08/04/2017" Auth="" Requestor="CVS - Lenexa" UserGUID="{788888-4555-4444-5455-55555}" GetUserGUIDDataCalledBy="Oreser" WebUserID="254282" UserName="TREW" ChangeOps="0" OrderID="1803019" OrderGUID="{6454546-ABD0-91CA-841D75A363CB}" Origin="W" OrderNumber="2718140" OrderDateTime="08/04/2017 10:00" CreateDate="08/09/17 00:31" DimWeightFactor="0" Pieces="1" Weight="0" RouteZone="LAX" CustomerGroup="" ServiceID="0" TotalMiles="0" AmountCharged="$0.00" DriverPayXML="1" CarrierTypeID="1" DueDateTime="8/4/2017 10:00:00 AM" PickedUpDateTime="" DeliveredDateTime="" OperationalStatus="N" saveorderpieceactionstatus="0" AmountChargedChanged="True">
<Stops>
<Stop Sequence="1" StopType="P" Note="" Name="UPS" Address="Adreena St" City="Vansrn NIRC" State="CA" Zip="98741" Phone="" EarlyDateTime="" ScheduledDateTime="8/4/2017 10:00:00 AM" LateDateTime="" AVSQuality="1" Latitude="74.213827" Longitude="-418.481529" DispatchZoneFound="True" Pieces="1" Weight="0">
<OrderStopPieces>
<OrderStopPiece Sequence="1" PieceAction="P" PieceID="714" OrderStopPieceID="1444"/>
</OrderStopPieces>
</Stop>
<Stop Sequence="2" StopType="D" Note="" Name="CCR Nicla" Address="S.BOSE STREET" City="ARI GUTS" State="CA" Zip="94125" Phone="1 (800) 360-0520" EarlyDateTime="" ScheduledDateTime="8/4/2017 10:00:00 AM" LateDateTime="" AVSQuality="1" Latitude="24.201719" Longitude="-17.491973" DispatchZoneFound="True" Pieces="0" Weight="0">
<OrderStopPieces>
<OrderStopPiece Sequence="1" PieceAction="D" PieceID="714" OrderStopPieceID="144"/>
</OrderStopPieces>
</Stop>
</Stops>
<Pieces>
<Piece Sequence="1" Pieces="1" Weight="" Description="" ContainerReference="" Reference="1111" PieceID="1111"/>
</Pieces>
<OrderEvents>
<OrderEvent EventType="MYOrder" Note="Test" Add="True" EventDateTime="8/9/2017 12:31:08 AM" UserName="MMPIO"/>
<OrderEvent EventType="New" Note="" EventDateTime="8/9/2017 12:21:08 AM" UserName="MMPIO"/>
</OrderEvents>
<Site SiteID="13" CompanyID="1008" SiteCode="MMM" SiteType="C" Name="FOO" DBAName="KMI" Address="JHHHH" City="ARIZoNA" State="AR" Zip="125487" Message=" MUT" ARAccountNumber="15470" ARDeptNumber="741" APAccountNumber="14720" APDeptNumber="1" BankAccountNumber="5001" BankDeptNumber="1" DispatchNote="" PaymentGatewayID="0" MName="" MAddress="" MCity="" MState="" MZip="" LogoImageID="0" SiteStatus="C"/>
<OrderFees>
<OrderFee FeeTitle="TS" FeeCode="VS"/>
<OrderFee FeeTitle="WEEE DDEA" FeeCode="QE"/>
</OrderFees>
<OrderNotifies/>
</Order>
</SOAP:Body>
Class for Deserialize
[XmlRoot(ElementName="OrderStopPiece")]
public class OrderStopPiece {
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="PieceAction")]
public string PieceAction { get; set; }
[XmlAttribute(AttributeName="PieceID")]
public string PieceID { get; set; }
[XmlAttribute(AttributeName="OrderStopPieceID")]
public string OrderStopPieceID { get; set; }
}
[XmlRoot(ElementName="OrderStopPieces")]
public class OrderStopPieces {
[XmlElement(ElementName="OrderStopPiece")]
public OrderStopPiece OrderStopPiece { get; set; }
}
[XmlRoot(ElementName="Stop")]
public class Stop {
[XmlElement(ElementName="OrderStopPieces")]
public OrderStopPieces OrderStopPieces { get; set; }
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="StopType")]
public string StopType { get; set; }
[XmlAttribute(AttributeName="Note")]
public string Note { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="Address")]
public string Address { get; set; }
[XmlAttribute(AttributeName="City")]
public string City { get; set; }
[XmlAttribute(AttributeName="State")]
public string State { get; set; }
[XmlAttribute(AttributeName="Zip")]
public string Zip { get; set; }
[XmlAttribute(AttributeName="Phone")]
public string Phone { get; set; }
[XmlAttribute(AttributeName="EarlyDateTime")]
public string EarlyDateTime { get; set; }
[XmlAttribute(AttributeName="ScheduledDateTime")]
public string ScheduledDateTime { get; set; }
[XmlAttribute(AttributeName="LateDateTime")]
public string LateDateTime { get; set; }
[XmlAttribute(AttributeName="AVSQuality")]
public string AVSQuality { get; set; }
[XmlAttribute(AttributeName="Latitude")]
public string Latitude { get; set; }
[XmlAttribute(AttributeName="Longitude")]
public string Longitude { get; set; }
[XmlAttribute(AttributeName="DispatchZoneFound")]
public string DispatchZoneFound { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string Pieces { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
}
[XmlRoot(ElementName="Stops")]
public class Stops {
[XmlElement(ElementName="Stop")]
public List<Stop> Stop { get; set; }
}
[XmlRoot(ElementName="Piece")]
public class Piece {
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string Pieces { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
[XmlAttribute(AttributeName="Description")]
public string Description { get; set; }
[XmlAttribute(AttributeName="ContainerReference")]
public string ContainerReference { get; set; }
[XmlAttribute(AttributeName="Reference")]
public string Reference { get; set; }
[XmlAttribute(AttributeName="PieceID")]
public string PieceID { get; set; }
}
[XmlRoot(ElementName="Pieces")]
public class Pieces {
[XmlElement(ElementName="Piece")]
public Piece Piece { get; set; }
}
[XmlRoot(ElementName="OrderEvent")]
public class OrderEvent {
[XmlAttribute(AttributeName="EventType")]
public string EventType { get; set; }
[XmlAttribute(AttributeName="Note")]
public string Note { get; set; }
[XmlAttribute(AttributeName="Add")]
public string Add { get; set; }
[XmlAttribute(AttributeName="EventDateTime")]
public string EventDateTime { get; set; }
[XmlAttribute(AttributeName="UserName")]
public string UserName { get; set; }
}
[XmlRoot(ElementName="OrderEvents")]
public class OrderEvents {
[XmlElement(ElementName="OrderEvent")]
public List<OrderEvent> OrderEvent { get; set; }
}
[XmlRoot(ElementName="Site")]
public class Site {
[XmlAttribute(AttributeName="SiteID")]
public string SiteID { get; set; }
[XmlAttribute(AttributeName="CompanyID")]
public string CompanyID { get; set; }
[XmlAttribute(AttributeName="SiteCode")]
public string SiteCode { get; set; }
[XmlAttribute(AttributeName="SiteType")]
public string SiteType { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="DBAName")]
public string DBAName { get; set; }
[XmlAttribute(AttributeName="Address")]
public string Address { get; set; }
[XmlAttribute(AttributeName="City")]
public string City { get; set; }
[XmlAttribute(AttributeName="State")]
public string State { get; set; }
[XmlAttribute(AttributeName="Zip")]
public string Zip { get; set; }
[XmlAttribute(AttributeName="Message")]
public string Message { get; set; }
[XmlAttribute(AttributeName="ARAccountNumber")]
public string ARAccountNumber { get; set; }
[XmlAttribute(AttributeName="ARDeptNumber")]
public string ARDeptNumber { get; set; }
[XmlAttribute(AttributeName="APAccountNumber")]
public string APAccountNumber { get; set; }
[XmlAttribute(AttributeName="APDeptNumber")]
public string APDeptNumber { get; set; }
[XmlAttribute(AttributeName="BankAccountNumber")]
public string BankAccountNumber { get; set; }
[XmlAttribute(AttributeName="BankDeptNumber")]
public string BankDeptNumber { get; set; }
[XmlAttribute(AttributeName="DispatchNote")]
public string DispatchNote { get; set; }
[XmlAttribute(AttributeName="PaymentGatewayID")]
public string PaymentGatewayID { get; set; }
[XmlAttribute(AttributeName="MName")]
public string MName { get; set; }
[XmlAttribute(AttributeName="MAddress")]
public string MAddress { get; set; }
[XmlAttribute(AttributeName="MCity")]
public string MCity { get; set; }
[XmlAttribute(AttributeName="MState")]
public string MState { get; set; }
[XmlAttribute(AttributeName="MZip")]
public string MZip { get; set; }
[XmlAttribute(AttributeName="LogoImageID")]
public string LogoImageID { get; set; }
[XmlAttribute(AttributeName="SiteStatus")]
public string SiteStatus { get; set; }
}
[XmlRoot(ElementName="OrderFee")]
public class OrderFee {
[XmlAttribute(AttributeName="FeeTitle")]
public string FeeTitle { get; set; }
[XmlAttribute(AttributeName="FeeCode")]
public string FeeCode { get; set; }
}
[XmlRoot(ElementName="OrderFees")]
public class OrderFees {
[XmlElement(ElementName="OrderFee")]
public List<OrderFee> OrderFee { get; set; }
}
[XmlRoot(ElementName="Order")]
public class Order {
[XmlElement(ElementName="Stops")]
public Stops Stops { get; set; }
[XmlElement(ElementName="Pieces")]
public Pieces Pieces { get; set; }
[XmlAttribute(AttributeName="Pieces")]
public string _Pieces { get; set; }
[XmlElement(ElementName="OrderEvents")]
public OrderEvents OrderEvents { get; set; }
[XmlElement(ElementName="Site")]
public Site Site { get; set; }
[XmlElement(ElementName="OrderFees")]
public OrderFees OrderFees { get; set; }
[XmlElement(ElementName="OrderNotifies")]
public string OrderNotifies { get; set; }
[XmlAttribute(AttributeName="Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName="PackageTypeID")]
public string PackageTypeID { get; set; }
[XmlAttribute(AttributeName="Service")]
public string Service { get; set; }
[XmlAttribute(AttributeName="CallerPhone")]
public string CallerPhone { get; set; }
[XmlAttribute(AttributeName="CustomerCode")]
public string CustomerCode { get; set; }
[XmlAttribute(AttributeName="Caller")]
public string Caller { get; set; }
[XmlAttribute(AttributeName="CalledBy")]
public string CalledBy { get; set; }
[XmlAttribute(AttributeName="CheckPiecesWeight")]
public string CheckPiecesWeight { get; set; }
[XmlAttribute(AttributeName="ForceReRateFlag")]
public string ForceReRateFlag { get; set; }
[XmlAttribute(AttributeName="RouteNumber")]
public string RouteNumber { get; set; }
[XmlAttribute(AttributeName="OrderDate")]
public string OrderDate { get; set; }
[XmlAttribute(AttributeName="Auth")]
public string Auth { get; set; }
[XmlAttribute(AttributeName="Requestor")]
public string Requestor { get; set; }
[XmlAttribute(AttributeName="UserGUID")]
public string UserGUID { get; set; }
[XmlAttribute(AttributeName="GetUserGUIDDataCalledBy")]
public string GetUserGUIDDataCalledBy { get; set; }
[XmlAttribute(AttributeName="WebUserID")]
public string WebUserID { get; set; }
[XmlAttribute(AttributeName="UserName")]
public string UserName { get; set; }
[XmlAttribute(AttributeName="ChangeOps")]
public string ChangeOps { get; set; }
[XmlAttribute(AttributeName="OrderID")]
public string OrderID { get; set; }
[XmlAttribute(AttributeName="OrderGUID")]
public string OrderGUID { get; set; }
[XmlAttribute(AttributeName="Origin")]
public string Origin { get; set; }
[XmlAttribute(AttributeName="OrderNumber")]
public string OrderNumber { get; set; }
[XmlAttribute(AttributeName="OrderDateTime")]
public string OrderDateTime { get; set; }
[XmlAttribute(AttributeName="CreateDate")]
public string CreateDate { get; set; }
[XmlAttribute(AttributeName="DimWeightFactor")]
public string DimWeightFactor { get; set; }
[XmlAttribute(AttributeName="Weight")]
public string Weight { get; set; }
[XmlAttribute(AttributeName="RouteZone")]
public string RouteZone { get; set; }
[XmlAttribute(AttributeName="CustomerGroup")]
public string CustomerGroup { get; set; }
[XmlAttribute(AttributeName="ServiceID")]
public string ServiceID { get; set; }
[XmlAttribute(AttributeName="TotalMiles")]
public string TotalMiles { get; set; }
[XmlAttribute(AttributeName="AmountCharged")]
public string AmountCharged { get; set; }
[XmlAttribute(AttributeName="DriverPayXML")]
public string DriverPayXML { get; set; }
[XmlAttribute(AttributeName="CarrierTypeID")]
public string CarrierTypeID { get; set; }
[XmlAttribute(AttributeName="DueDateTime")]
public string DueDateTime { get; set; }
[XmlAttribute(AttributeName="PickedUpDateTime")]
public string PickedUpDateTime { get; set; }
[XmlAttribute(AttributeName="DeliveredDateTime")]
public string DeliveredDateTime { get; set; }
[XmlAttribute(AttributeName="OperationalStatus")]
public string OperationalStatus { get; set; }
[XmlAttribute(AttributeName="saveorderpieceactionstatus")]
public string Saveorderpieceactionstatus { get; set; }
[XmlAttribute(AttributeName="AmountChargedChanged")]
public string AmountChargedChanged { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="Order")]
public Order Order { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="SOAP", Namespace="http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
my code :
// response xml in str
XmlSerializer serializer = new XmlSerializer(typeof(SaveResponseObject.Envelope));
using (TextReader reader = new StringReader(str))
{
SaveResponseObject.Envelope result = (SaveResponseObject.Envelope)serializer.Deserialize(reader);
}
Add Namespace = "" to Order property in Body class. Should be ok.
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "Order", Namespace = "")]
public Order Order { get; set; }
}
Use this attribute to decorate over order
[XmlElement(ElementName = "Order", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
I have some problems deserializing the following JSON string. On all of my other responses it works really good but not for this one. I tried so many different things and I checked the Stream class about 100 times. I also have used a generated class from json2csharp but all my deserialized Data is NULL.
My code to get the response:
public Stream getStream(string userID)
{
var request = GetRequest("streams/{channelID}", Method.GET);
request.AddUrlSegment("channelID", userID);
var response = restClient.Execute<Stream>(request);
return response.Data;
}
response.Content shows me as example something like that:
{
"stream":
{
"_id":25649270464,
"game":"test",
"broadcast_platform":"other",
"community_id":"xxxxxxxxx",
"community_ids":["xxxxxx"],
"viewers":2421,
"video_height":1080,
"average_fps":61.7876424715,
"delay":0,
"created_at":"2017-07-02T10:01:28Z",
"is_playlist":false,
"stream_type":"live",
"preview":
{
"small":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-80x45.jpg",
"medium":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-320x180.jpg",
"large":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-640x360.jpg",
"template":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-{width}x{height}.jpg"
},
"channel":
{
"mature":false,
"status":"Test"
,"broadcaster_language":"de",
"display_name":"LOSTAIMING",
"game":"test",
"language":"en",
"_id":44281267,
"name":"lostaiming",
"created_at":"2013-06-02T16:42:19.329009Z",
"updated_at":"2017-07-02T13:05:11.555285Z",
"partner":true,
"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/lostaiming-profile_image-e9d7ea0893748d6a-300x300.png",
"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/cc34c6b909a435ae-channel_offline_image-1920x1080.png",
"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/c187e8871c0f6a2b-profile_banner-480.png",
"profile_banner_background_color":"",
"url":"https://www.twitch.tv/lostaiming",
"views":2292173,
"followers":55672,
"broadcaster_type":"",
"description":"Blubb"
}
}
}
And my Stream class looks like this:
class Stream
{
[JsonProperty("stream")]
public SubStream stream { get; set; }
}
class SubStream
{
[JsonProperty("_id")]
public string ID { get; set; }
[JsonProperty("game")]
public string Game { get; set; }
[JsonProperty("broadcast_platform")]
public string BroadcastPlatform { get; set; }
[JsonProperty("community_id")]
public string CommunityID { get; set; }
[JsonProperty("community_ids")]
public List<object> CommunityIDS { get; set; }
[JsonProperty("viewers")]
public long Viewers { get; set; }
[JsonProperty("video_height")]
public long VideoHeigt { get; set; }
[JsonProperty("average_fps")]
public double AverageFps { get; set; }
[JsonProperty("delay")]
public long Delay { get; set; }
[JsonProperty("createt_at")]
public DateTime CreatetAt { get; set; }
[JsonProperty("is_playlist")]
public bool IsPlaylist { get; set; }
[JsonProperty("stream_type")]
public string StreamType { get; set; }
[JsonProperty("preview")]
public StreamPreview Preview { get; set; }
[JsonProperty("channel")]
public Channel channel { get; set; }
}
class StreamPreview
{
[JsonProperty("small")]
public string SmallPreview { get; set; }
[JsonProperty("medium")]
public string MediumPreview { get; set; }
[JsonProperty("large")]
public string LargPreview { get; set; }
[JsonProperty("template")]
public string TemplatePreview { get; set; }
}
public class Channel
{
[JsonProperty("mature")]
public bool Mature { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("broadcaster_language")]
public string BroadcasterLanguage { get; set; }
[JsonProperty("display_name")]
public string DisplayName { get; set; }
[JsonProperty("game")]
public string Game { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("_id")]
public string ID { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }
[JsonProperty("partner")]
public bool Partner { get; set; }
[JsonProperty("logo")]
public string Logo { get; set; }
[JsonProperty("video_banner")]
public string VideoBanner { get; set; }
[JsonProperty("profile_banner")]
public string ProfileBanner { get; set; }
[JsonProperty("profile_banner_background_color")]
public string ProfileBannerBackgroundColor { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("views")]
public long Views { get; set; }
[JsonProperty("followers")]
public long Followers { get; set; }
[JsonProperty("broadcaster_type")]
public string BroadcastType { get; set; }
[JsonProperty("stream_key")]
public string StreamKey { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
}
1) use JSON Utils (https://jsonutils.com/) to generate your C# classes (for attributes select JsonProperty)
2) Newtonsoft.Json is pretty much the standard best library to work with JSON formats in .NET, you can get it from nuget.
3) The code should look something like this, and deserialization should work without any problems:
class Program
{
private static void Main(string[] args)
{
var myObj = JsonConvert.DeserializeObject<JsonDef>(json);
Console.Read();
}
}
public class Preview
{
[JsonProperty("small")]
public string small { get; set; }
[JsonProperty("medium")]
public string medium { get; set; }
[JsonProperty("large")]
public string large { get; set; }
[JsonProperty("template")]
public string template { get; set; }
}
public class Channel
{
[JsonProperty("mature")]
public bool mature { get; set; }
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("broadcaster_language")]
public string broadcaster_language { get; set; }
[JsonProperty("display_name")]
public string display_name { get; set; }
[JsonProperty("game")]
public string game { get; set; }
[JsonProperty("language")]
public string language { get; set; }
[JsonProperty("_id")]
public int _id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("created_at")]
public DateTime created_at { get; set; }
[JsonProperty("updated_at")]
public DateTime updated_at { get; set; }
[JsonProperty("partner")]
public bool partner { get; set; }
[JsonProperty("logo")]
public string logo { get; set; }
[JsonProperty("video_banner")]
public string video_banner { get; set; }
[JsonProperty("profile_banner")]
public string profile_banner { get; set; }
[JsonProperty("profile_banner_background_color")]
public string profile_banner_background_color { get; set; }
[JsonProperty("url")]
public string url { get; set; }
[JsonProperty("views")]
public int views { get; set; }
[JsonProperty("followers")]
public int followers { get; set; }
[JsonProperty("broadcaster_type")]
public string broadcaster_type { get; set; }
[JsonProperty("description")]
public string description { get; set; }
}
public class Stream
{
[JsonProperty("_id")]
public long _id { get; set; }
[JsonProperty("game")]
public string game { get; set; }
[JsonProperty("broadcast_platform")]
public string broadcast_platform { get; set; }
[JsonProperty("community_id")]
public string community_id { get; set; }
[JsonProperty("community_ids")]
public IList<string> community_ids { get; set; }
[JsonProperty("viewers")]
public int viewers { get; set; }
[JsonProperty("video_height")]
public int video_height { get; set; }
[JsonProperty("average_fps")]
public double average_fps { get; set; }
[JsonProperty("delay")]
public int delay { get; set; }
[JsonProperty("created_at")]
public DateTime created_at { get; set; }
[JsonProperty("is_playlist")]
public bool is_playlist { get; set; }
[JsonProperty("stream_type")]
public string stream_type { get; set; }
[JsonProperty("preview")]
public Preview preview { get; set; }
[JsonProperty("channel")]
public Channel channel { get; set; }
}
public class JsonDef
{
[JsonProperty("stream")]
public Stream stream { get; set; }
}
It's because of the DateTime format of the JSON. It is not in a well-formed .Net DateTime. so you can get it as a string and then convert it the way you want or change its format into a readable .Net one.