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.
Related
I'm having problems deserializing the data I'm getting from Shodan. Below are the classes I got from json2csharp and I'm trying to create an array of the matches and loop through them. It seems like I have tried with everything except a working array by now. The data itself is matches as root with objects of them that contain location (with its own data etc). An except below that I cut out a bit.
This is my error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Shodan.Match[]' because the type requ
ires a JSON array (e.g. [1,2,3]) to deserialize correctly.
var data = JsonConvert.DeserializeObject<Match[]>(allData);
{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}
public class Location
{
public string city { get; set; }
public string region_code { get; set; }
public object area_code { get; set; }
public double longitude { get; set; }
public string country_code3 { get; set; }
public double latitude { get; set; }
public string postal_code { get; set; }
public object dma_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
}
public class Options
{
}
public class Shodan
{
public string crawler { get; set; }
public string id { get; set; }
public string module { get; set; }
public Options options { get; set; }
}
public class Match
{
public int hash { get; set; }
public int ip { get; set; }
public string isp { get; set; }
public string transport { get; set; }
public string data { get; set; }
public string asn { get; set; }
public int port { get; set; }
public List<string> hostnames { get; set; }
public Location location { get; set; }
public DateTime timestamp { get; set; }
public List<string> domains { get; set; }
public string org { get; set; }
public object os { get; set; }
public Shodan _shodan { get; set; }
public string ip_str { get; set; }
public string product { get; set; }
}
public class RootObject
{
public List<Match> matches { get; set; }
public int total { get; set; }
}
You could create another class like
var allData =
{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}
public class MyMatches {
public Match[] matches {get; set;}
}
and then use that in the deserializer.
var data = JsonConvert.DeserializeObject<MyMatches>(allData);
This is if the JSON code sample you gave us is correct.
CORRECTION
Just saw the RootObject class.
Just use that.
I'm attempting to deserialize the following json:
{"PatientNameID":{"ID":514,"Name":{"First":"Laura","Middle":"X","Last":"Coelho","Suffix":"","Full":"Laura X Coelho","Preferred":""}},"PatientNumber":"254","ChartNumber":"254","Gender":{"LookupType":"Gender","Code":"F","Description":"Female","Order":1,"Active":true,"AlternateCodes":null},"DOB":"4/9/1953","PhoneNumber":"3521029496","SSN":"*****0161"}
This is the class and subclasses into which I'm trying to deserialize the above JSON:
public class PatientList3
{
public Pat PatientNameID { get; set; }
public string PatientNumber { get; set; }
public string ChartNumber { get; set; }
public Gender2 Gender { get; set; }
public string DOB { get; set; }
public string PhoneNumber { get; set; }
public string SSN { get; set; }
}
public class Pat
{
public int ID { get; set; }
public PtName Name { get; set; }
}
public class PtName
{
public string First { get; set; }
public string Middle { get; set; }
public string Last { get; set; }
public string Suffix { get; set; }
public string Full { get; set; }
public string Preferred { get; set; }
}
public class Gender2
{
string LookupType { get; set; }
string Code { get; set; }
string Description { get; set; }
int Order { get; set; }
bool Active { get; set; }
List<AlternateCodes> AlternateCodes { get; set; }
}
public class AlternateCodes
{
string Code { get; set; }
string Description { get; set; }
string CodeSystem { get; set; }
string CodeSystemName { get; set; }
}
Everything goes well when I deserialize it except all of the values in the Gender2 class are null.
I've referred to the following two posts for answers but nothing seems to be doing to trick.
JsonConvert.DeserializeObject<T>(JsonString) returning all Properties<T> as Null
DeSerializing JSON returns null C#
Fix the properties on Gender2 & AlternateCodes they are not public! The deserializer will not be able to find your any of your properties so that is probably the reason this fails to populate.
The problem is with access modifiers of properties Gender2 and AlternateCodes class, default access modifiers for properties is private. You should change it to:
public class Gender2
{
public string LookupType { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public int Order { get; set; }
public bool Active { get; set; }
public List<AlternateCodes> AlternateCodes { get; set; }
}
public class AlternateCodes
{
public string Code { get; set; }
public string Description { get; set; }
public string CodeSystem { get; set; }
public string CodeSystemName { get; set; }
}
After setting properties to public it deserializes successfully:
I'm trying to work with AWS SDK in C# to follow and update a price catalog.
I'm using the method GetProductsAsync to list EC2 products for example, I then try to deserialize the response.
I use Json.Net to deserialize my response into a class I created using the "Paste JSON as classes" function from Visual Studio.
The object is somewhat populated, but the pricing model follows a weird JSON pattern.
Here is an extract of the object:
"terms":{
"OnDemand":{
"FBKCX9C4KX8NSVN3.JRTCKXETXF":{
"priceDimensions":{
"FBKCX9C4KX8NSVN3.JRTCKXETXF.6YS6EN2CT7":{
"unit":"Hrs",
"endRange":"Inf",
"description":"$2.47 per On Demand RHEL m4.10xlarge Instance Hour",
"appliesTo":[
],
"rateCode":"FBKCX9C4KX8NSVN3.JRTCKXETXF.6YS6EN2CT7",
"beginRange":"0",
"pricePerUnit":{
"USD":"2.4700000000"
}
}
},
The IDs under OnDemand and PriceDimensions seem to be references to other objects; therefore, they are not populated when I deserialize the JSON object, as they are different per product type.
Has anyone succeeded in getting pricing information for AWS assets?
For JSON objects having keys which can vary, you can use a Dictionary<string, T> in place of a regular class, where T is a class representing the item data. So in your case, you'd need a dictionary for both OnDemand and priceDimensions. The resulting class definitions would look like this:
public class OuterObject
{
public Terms terms { get; set; }
}
public class Terms
{
public Dictionary<string, OnDemandItem> OnDemand { get; set; }
}
public class OnDemandItem
{
public Dictionary<string, PriceDimensionsItem> priceDimensions { get; set; }
}
public class PriceDimensionsItem
{
public string unit { get; set; }
public string endRange { get; set; }
public string description { get; set; }
public object[] appliesTo { get; set; }
public string rateCode { get; set; }
public string beginRange { get; set; }
public PricePerUnit pricePerUnit { get; set; }
}
public class PricePerUnit
{
public string USD { get; set; }
}
Demo: https://dotnetfiddle.net/dJ5jmQ
Note: you could also use a Dictionary<string, string> in place of the PricePerUnit class if you will be dealing with a lot of different currencies. If there will just one or two, then having a strongly-typed class with properties for each possible currency will work fine. For example, you could add a property public string EUR { get; set; } to handle Euro.
AWS SDK have one more "level" after OnDemand.
I made my own class based on Brian Rogers answer, and i add the rest of the class to support Reserved and Products, making a nested class.
public class OuterObject
{
public Dictionary<string, Products> products { get; set; }
public Terms terms { get; set; }
}
public class Products
{
public string sku { get; set; }
public string productFamily { get; set; }
public Attributes attributes { get; set; }
}
public class Attributes
{
public string servicecode { get; set; }
public string location { get; set; }
public string locationType { get; set; }
public string instanceType { get; set; }
public string currentGeneration { get; set; }
public string vcpu { get; set; }
public string memory { get; set; }
public string operatingSystem { get; set; }
public string licenseModel { get; set; }
public string preInstalledSw { get; set; }
public string tenancy { get; set; }
}
public class Terms
{
public Dictionary<string, Dictionary<string, OnDemandItem>> OnDemand { get; set; }
public Dictionary<string, Dictionary<string, OnDemandItem>> Reserved { get; set; }
}
public class OnDemandItem
{
public string offerTermCode { get; set; }
public string sku { get; set; }
public Dictionary<string, PriceDimensionsItem> priceDimensions { get; set; }
public TermAttributes termAttributes { get; set; }
}
public class PriceDimensionsItem
{
public string unit { get; set; }
public string endRange { get; set; }
public string description { get; set; }
public object[] appliesTo { get; set; }
public string rateCode { get; set; }
public string beginRange { get; set; }
public PricePerUnit pricePerUnit { get; set; }
}
public class PricePerUnit
{
public string USD { get; set; }
}
public class TermAttributes
{
public string LeaseContractLength { get; set; }
public string OfferingClass { get; set; }
public string PurchaseOption { get; set; }
}
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}
}
sorry for a dumb question but I haven't found any solution for this.
Here is my JSON:
{
"Id": "1",
"Parent.Id": "1",
"Agent.Id": "1",
"Agent.Profile.FullName": "gena",
"Fee": "10.1200",
"FeeManagementDate": "29/11/2013",
"Contact.Name": "Genady",
"Contact.Telephone": "000000000",
"Contact.Email": "gena#email.com",
"AgreementUrl": "http://www.test.com/agreement"
}
Here is my object
public class ManagementDetailsViewModel : ViewModel<int> {
public ManagementDetailsViewModel() {
}
public string AgreementUrl { get; set; }
public HttpPostedFileBase AgreementFile { get; set; }
public decimal Fee { get; set; } // payment data
public DateTime? FeeDate { get; set; }
public string FeeManagementDate {
get { return FeeDate != null ? FeeDate.Value.ToString("dd/MM/yyyy") : DateTime.Now.ToString("dd/MM/yyyy"); }
set {
FeeDate = Convert.ToDateTime(value);
}
}
public BusinessViewModel Parent { get; set; }
public MemberViewModel Agent { get; set; }
public Contact Contact { get; set; }
}
How do I convert the json string to object (with inner objects)?
Json.Net needs some help because your json object contain propery names, which is not valid in c# like(Agent.Id)
var obj = JsonConvert.DeserializeObject<MyObj>(json);
How do I convert the json string to object (with inner objects)?
Since your json is flat(not containing sub objects) you have to post process your deserialized object if you want to use it that way/
public class MyObj
{
public string Id { get; set; }
[JsonProperty("Parent.Id")]
public string ParentId { get; set; }
[JsonProperty("Agent.Id")]
public string AgentId { get; set; }
[JsonProperty("Agent.Profile.FullName")]
public string ProfileFullName { get; set; }
public string Fee { get; set; }
public string FeeManagementDate { get; set; }
[JsonProperty("Contact.Name")]
public string ContactName { get; set; }
[JsonProperty("Contact.Telephone")]
public string ContactTelephone { get; set; }
[JsonProperty("Contact.Email")]
public string ContactEmail { get; set; }
public string AgreementUrl { get; set; }
}