I have a C# classes and I need to parse JSON into it.
The class has a List<> from another class.
The class structure is like this.
public class OrderFund {
public int OrderID { get; set; }
public int BrokerID { get; set; }
public string SettlementMethod { get; set; }
public List<SettlementSap> SettlementsSap { get; set; }
}
public class SettlementSap {
public string SapMonetaryAccountNo { get; set; }
public string SapMonetaryAccountType { get; set; }
public string SapMonetaryAccountOffice { get; set; }
}
My JSON is like this.
{
"settlementMethod": "SAP",
"BrokerID": 1,
"OrderID": 1,
"Settlements": [
{
"SapMonetaryAccountNo": "400245892464",
"SapMonetaryAccountType": "CA",
"SapMonetaryAccountOffice": "AR"
}
]
}
I load my JSON file like this...
static OrderFund LoadJson(string file) {
string dire = Directory.GetCurrentDirectory();
using (StreamReader r = new StreamReader(dire + "\\" + file)) {
string json = r.ReadToEnd();
OrderFund items = JsonConvert.DeserializeObject<OrderFund>(json);
return items;
}
}
The data load fine into OrderFun Class but OrderFund.SettlementsSap is null.
How can I load Settlements into SettlementsSap?
That's because you have named the field SettlementsSap but your Json field is called Settlements...
You could rename the field in your class;
public class OrderFund
{
public int OrderID { get; set; }
public int BrokerID { get; set; }
public string SettlementMethod { get; set; }
public List<SettlementSap> Settlements { get; set; }
}
or add a [JsonProperty("Settlements")]
attribute to the field like so;
public class OrderFund
{
public int OrderID { get; set; }
public int BrokerID { get; set; }
public string SettlementMethod { get; set; }
[JsonProperty("Settlements")]
public List<SettlementSap> SettlementsSap { get; set; }
}
You just use a function of Visual Studio which convert your json into a model class
Goto: Edit -> Paste special -> Paste JSON as Class
The model class created by this feature will solve your problem
So, visiblely, you must rename SettlementsSap by Settlements
public class OrderFund
{
public string settlementMethod { get; set; }
public int BrokerID { get; set; }
public int OrderID { get; set; }
public Settlement[] Settlements { get; set; }
}
public class Settlement
{
public string SapMonetaryAccountNo { get; set; }
public string SapMonetaryAccountType { get; set; }
public string SapMonetaryAccountOffice { get; set; }
}
The problem is with the naming. In the JSON, the name is Settlements. But in the class definition of OrderFund it is named as SettlementsSap
Related
I currently have JSON coming in as follows:
{"36879":[{"min_qty":1,"discount_type":"%","csp_price":10}],"57950":[{"min_qty":1,"discount_type":"flat","csp_price":650}]}
This contains a list of the following records
ProductId
MinQty
DiscountType
Price
I need to deserialize this into the following model:
public class CustomerSpecificPricing
{
string productId { get; set; }
public virtual List<CustomerSpecificPricingDetail> CustomerSpecificPricingDetails { get; set; }
}
public class CustomerSpecificPricingDetail
{
public string min_qty { get; set; }
public string discount_type { get; set; }
public string csp_price { get; set; }
}
The problem is that the "productId" of each record is missing the key name.
If I run my JSON through J2C, I get the following:
public class 36879 {
public int min_qty { get; set; }
public string discount_type { get; set; }
public int csp_price { get; set; }
}
public class 57950 {
public int min_qty { get; set; }
public string discount_type { get; set; }
public int csp_price { get; set; }
}
public class Root {
public List<_36879> _36879 { get; set; }
public List<_57950> _57950 { get; set; }
}
Which is obviously incorrect.
How would I deserialize my object correctly?
You would need to deserialize it into a dictionary first and then map it into the format you require after. Something like this should work:
var dict = JsonConvert.DeserializeObject<Dictionary<string, IEnumerable<CustomerSpecificPricingDetail>>>();
var result = dict.Select(kvp => new CustomerSpecificPricing { ProductId = Int32.Parse(kvp.Key), CustomerSpecificPricingDetails = kvp.Value });
Id also recommend you follow the conventional standards of naming. In this case properties in classes should be PascalCase,
e.g. your classes now become:
public class CustomerSpecificPricing
{
[JsonProperty("productId ")]
public string ProductId { get; set; }
public virtual List<CustomerSpecificPricingDetail> CustomerSpecificPricingDetails { get; set; }
}
and
public class CustomerSpecificPricingDetail
{
[JsonProperty("min_qty")]
public string MinQty { get; set; }
[JsonProperty("discount_type ")]
public string DiscountType { get; set; }
[JsonProperty("csp_price ")]
public string CspPrice { get; set; }
}
I have the following Json returned from iTunes (I have replaced some details for privacy such as username and review content)
{"feed":{"author":{"name":{"label":"iTunes Store"}, "uri":{"label":"http://www.apple.com/uk/itunes/"}}, "entry":[
{"author":{"uri":{"label":"https://itunes.apple.com/gb/reviews/ID"}, "name":{"label":"Username"}, "label":""}, "im:version":{"label":"3.51"}, "im:rating":{"label":"4"}, "id":{"label":"12345"}, "title":{"label":"Review title"}, "content":{"label":"Review contents", "attributes":{"type":"text"}}, "link":{"attributes":{"rel":"related", "href":"https://itunes.apple.com/gb/review?reviewid"}}, "im:voteSum":{"label":"0"}, "im:contentType":{"attributes":{"term":"Application", "label":"Application"}}, "im:voteCount":{"label":"0"}},
// more entries ...
I want to deserialize this into a class. I only need the Review Title, Review contents, and the "im:rating" (which is the number of stars). However, I'm struggling due to the nested Json and the use of keys of how to extract this data. So far I have created a class ready to deserialize, and I have AppStoreData appStoreData = JsonConvert.DeserializeObject<AppStoreData>(stringResult); to deserialize it
public class AppStoreData {
}
My problem is that I don't know what to put inside the class to get the info I need from the Json. I have tried things such as:
public string title {get; set;}
public string content {get; set;}
public string imrating {get; set;}
However this doesn't work. I also think im:rating is an invalid name in C#.
Can anyone please help? Thank you
To get around the issue with im:rating you can use a JsonProperty attribute
[JsonProperty("im:rating")]
public Id ImRating { get; set; }
The issue with converting things like Label to string property is that it's not a string but an object in the input file.
You need something like
[JsonProperty("title")]
public Id Title { get; set; }
where Id is a class
public class Id
{
[JsonProperty("label")]
public string Label { get; set; }
}
Or write a deserializer that converts it to string for you.
I would suggest trying out one of many free code generators like https://app.quicktype.io/?l=csharp or http://json2csharp.com/ to get a headstart and edit everything to your liking afterwards.
I suggest using json2csharp to convert the json to c# model and change the invalid attributes by adding JsonProperty
public class Name
{
public string label { get; set; }
}
public class Uri
{
public string label { get; set; }
}
public class Author
{
public Name name { get; set; }
public Uri uri { get; set; }
}
public class Uri2
{
public string label { get; set; }
}
public class Name2
{
public string label { get; set; }
}
public class Author2
{
public Uri2 uri { get; set; }
public Name2 name { get; set; }
public string label { get; set; }
}
public class ImVersion
{
public string label { get; set; }
}
public class ImRating
{
public string label { get; set; }
}
public class Id
{
public string label { get; set; }
}
public class Title
{
public string label { get; set; }
}
public class Attributes
{
public string type { get; set; }
}
public class Content
{
public string label { get; set; }
public Attributes attributes { get; set; }
}
public class Attributes2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Link
{
public Attributes2 attributes { get; set; }
}
public class ImVoteSum
{
public string label { get; set; }
}
public class Attributes3
{
public string term { get; set; }
public string label { get; set; }
}
public class ImContentType
{
public Attributes3 attributes { get; set; }
}
public class ImVoteCount
{
public string label { get; set; }
}
public class Entry
{
public Author2 author { get; set; }
[JsonProperty(PropertyName = "im:version")]
public ImVersion imversion { get; set; }
[JsonProperty(PropertyName = "im:rating")]
public ImRating imrating { get; set; }
public Id id { get; set; }
public Title title { get; set; }
public Content content { get; set; }
public Link link { get; set; }
[JsonProperty(PropertyName = "im:voteSum")]
public ImVoteSum imvoteSum { get; set; }
[JsonProperty(PropertyName = "im:contentType")]
public ImContentType imcontentType { get; set; }
[JsonProperty(PropertyName = "im:voteCount")]
public ImVoteCount imvoteCount { get; set; }
}
public class Feed
{
public Author author { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject5
{
public Feed feed { get; set; }
}
Once the model is ready you can use JsonConvert.DeserializeObject to convert the JSON to c# object
using (StreamReader r = new StreamReader(filepath)) {
string json = r.ReadToEnd();
var newEmps = JsonConvert.DeserializeObject<RootObject5>(json);
Console.WriteLine(newEmps.feed.entry[0].imvoteCount.label);
}
My JSON file
[
{
"amount":"1000000.0",
"check_number":1,
"payment_number":5,
"attachments":[
{
"id":5324,
"url":"http://www.example.com/",
"filename":"january_receipt_copy.jpg"
}
]
}
]
My Class File
public class Attachment
{
public int id { get; set; }
public string url { get; set; }
public string filename { get; set; }
}
public class AccountDetail
{
public string amount { get; set; }
public int check_number { get; set; }
public int payment_number { get; set; }
}
public class RootObject
{
public AccountDetail accountdetail{ get; set; }
public List<Attachment> attachments { get; set; }
}
Now I want to map JSON file's properties 'check_number','amount' etc
to accountdetail by using newtonsoft JSON deserialization.
You need the following two classes:
public class Attachment
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("filename")]
public string Filename { get; set; }
}
public class AccountDetails
{
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("check_number")]
public int CheckNumber { get; set; }
[JsonProperty("payment_number")]
public int PaymentNumber { get; set; }
[JsonProperty("attachments")]
public IList<Attachment> Attachments { get; set; }
}
By defining the above classes you can deserialize your json as below:
var accountsDetails = JsonConvert.DeserializeObject<IEnumerable<AccountDetails>>(json);
I'm trying to GET a JSON REST-api request, deserialize it using the Newtonsoft.Json package for .NET and access methods within the newly created object, but I keep getting an error that won't let me run my C# code in Visual Studio 2015.
For the following JSON string,
{
"pagination":
{
"per_page": 1,
"items": 28,
"page": 1,
"urls":
{
"last": "https://...",
"next": "https://..."
},
"pages": 28
},
"results":
[{
"style": ["House"],
"thumb": "https://...",
"format": ["File", "AAC", "Album"],
"country": "Unknown",
"barcode": ["id886037928"],
"uri": "/Porter-Robinson-Worlds/master/721049",
"community": {"have": 932, "want": 720},
"label": ["Astralwerks", "Sample Sized, LLC", "Astralwerks"],
"catno": "none",
"year": "2014",
"genre": ["Electronic"],
"title": "Porter Robinson - Worlds",
"resource_url": "https://...",
"type": "master",
"id": 721049
}]
}
I created the following C# object class:
public class Discogs
{
public class pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public class urls
{
public string last { get; set; }
public string next { get; set; }
}
public int pages { get; set; }
public class data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public class community
{
public string have { get; set; }
public string want { get; set; }
}
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class results
{
public data Results { get; set; }
}
}
}
In a private async void class, I've successfully fetched the GET request and stored it in the string, jsonstring. Now I try to run this code:
Discogs myUser = new Discogs();
myUser = JsonConvert.DeserializeObject<Discogs>(jsonstring);
int yr = myUser.pagination.data.year;
...but my project gets an error, An object reference is required for the non-static field, method, or property 'Discogs.pagination.data.year', cannot access non-static property 'year' in static context.
This does not make sense to me because I have no static classes or methods. I've searched for a solution but all similar problems seem to be able to access deserialized objects without any such error. Any help on accessing the methods in my Discogs object would be greatly appreciated.
The problem is that you are trying to use the class Pagination without instantiating the class. In order to use a non-static class (Pagination, Data, Community) you must first instantiate them like below
Pagination pag = new Pagination();
Your structure here is pretty odd. Normally classes would be in separate files or at least not nested such as you have here. You may want to rethink the way you've designed this program.
You are trying to get value from "myUser.pagination...", but in your example "pagination" is a class name not a property inside "Discogs" class, same as "data" inside "pagination" class
code with nested classes:
public class Discogs
{
public class Pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public class Urls
{
public string last { get; set; }
public string next { get; set; }
}
public Urls urls {get;set;}
public int pages { get; set; }
public class Data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public class Community
{
public string have { get; set; }
public string want { get; set; }
}
public Community community { get; set; }
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class Results
{
public Data Results { get; set; }
}
public Results result {get;set;}
}
public Pagination pagination {get;set}
}
code with i think a bit easy to understand:
public class Urls
{
public string last { get; set; }
public string next { get; set; }
}
public class Community
{
public string have { get; set; }
public string want { get; set; }
}
public class Data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
public Community community { get; set; }
}
public class Results
{
public Data Results { get; set; }
}
public class Pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public int pages { get; set; }
public Urls urls {get;set;}
public Results result {get;set;}
}
public class Discogs
{
public Pagination pagination {get;set}
}
I have this specific JSON response that I am trying to deserialize without success. I am hoping someone can help me.
Here is the JSON response I get:
{
"num_locations": 1,
"locations": {
"98765": {
"street1": "123 Fake Street",
"street2": "",
"city": "Lawrence",
"state": "Kansas",
"postal_code": "66044",
"s_status": "20",
"system_state": "Off"
}
}
}
I used json2csharp http://json2csharp.com and got these recommended classes:
public class __invalid_type__98765
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
public class Locations
{
public __invalid_type__98765 __invalid_name__98765 { get; set; }
}
public class RootObject
{
public int num_locations { get; set; }
public Locations locations { get; set; }
}
But when I try to use it in my code:
var locationResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);
What I get is (Watch):
locationResponse : {RestSharpConsoleApplication.Program.RootObject} : RestSharpConsoleApplication.Program.RootObject
locations : {RestSharpConsoleApplication.Program.Locations} : RestSharpConsoleApplication.Program.Locations
__invalid_name__98765 : null : RestSharpConsoleApplication.Program.__invalid_type__98765
num_locations : 1 : int
Obviously I am not creating (json2csharp) the right classes for the DeserializeObject, and sadly I have no control over the JSON response (vendor = SimpliSafe).
It is obvious the "98765" is meant to be a value (location number) but json2csharp makes it into this __invalid_type__98765 class and this is probably why it gets null.
Any idea how should the classes look for this particular JSON to be successfully deserialized?
Thanks!
Zachs
You should be able to do this with a dictionary:
public class MyData{
[JsonProperty("locations")]
public Dictionary<string, Location> Locations {get;set;}
}
public class Location
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
Do you have a non-Express version of Visual Studio? If so, copy the JSON to clipboard and then go to the Visual Studio menu: Edit >> Paste special >> Paste JSON as classes.
Using that gives:
public class Rootobject {
public int num_locations { get; set; }
public Locations locations { get; set; }
}
public class Locations {
public _98765 _98765 { get; set; }
}
public class _98765 {
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
That suggests your JSON structure is not quite right.
You can also specify the property name via an attribute to use to get around this:
public class RootObject
{
public int num_locations { get; set; }
public Locations locations { get; set; }
}
public class Locations
{
[JsonProperty("98765")]
public LocationInner Inner { get; set; }
}
public class LocationInner
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
...but it would really be better if the JSON were properly formatted such that the Locations was actually an array of location objects.