Can json.net serialize class objects into a simple dictionary?
public class CommitData
{
public AddressDTO Property { get; set; }
public DateTime? Appointment { get; set; }
public DateTime? Closed { get; set; }
public DateTime? Created { get; set; }
public string LenderRef { get; set; }
public string YearBuilt { get; set; }
public IDictionary<string, object> Custom { get; set; }
}
public class AddressDTO
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
}
When serializing I want to get:
{
"Property.Address1": "",
"Property.Address2": "uyg",
"LenderRef": "yguyg",
"Closed": date_here
}
And similarly deserialize a json string like above into the CommitData object?
Yes, but it is a bit of work. You have full control of how your object is serialized and deserialized by implementing your own JsonConverter. With that, you can flatten it and unflatten it by doing simple string manipulation. A proper generic solution could be acomplished but would take a lot more work as you would need to consider multiple levels of recursion on each property but if you only care about this case in particular then use Custom JsonConverter
Here is an example of a converter Json.net uses internally:
KeyValuePairConverter.cs
Hope it helps.
Related
I am trying to take this JSON:
{
"title":"string",
"description":"string",
"date":"2021-04-19T01:05:38.000Z",
"image":"url",
"images":[
"url1",
"url2"
],
"attributes":{
"phonebrand":"x",
"phonecarrier":"y",
"forsaleby":"z",
"price":12345,
"location":"daLocation",
"type":"OFFERED"
},
"url":"url to listing"
}
And convert it into this C# Object:
public class Listing {
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("date")]
public DateTime? Date { get; set; }
[JsonProperty("image")]
public string Image { get; set; }
[JsonProperty("images")]
public string[] Images { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("price")]
public decimal Price { get; set; }
[JsonProperty("locationId")]
public int LocationId { get; set; }
[JsonProperty("categoryId")]
public int CategoryId { get; set; }
[JsonProperty("sortByName")]
public string SortByName { get; set; }
[JsonProperty("q")]
public string Q { get; set; }
[JsonProperty("location")]
public string Location { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("forsaleby")]
public string ForSaleBy { get; set; }
[JsonProperty("fulfillment")]
public string Fulfillment { get; set; }
[JsonProperty("payment")]
public string Payment { get; set; }
[JsonProperty("phonebrand")]
public string? PhoneBrand { get; set; }
[JsonProperty("phonecarrier")]
public string? PhoneCarrier { get; set; }
}
My problem is, I'm trying to deserialize properties like price and phonebrand but those properties are under an object in the JSON. So when I try to deserialize them like this, those properties can't be found and are set as null. How can I deserialize those properties without changing my C# Class to include an Attributes class? I want to do this because I think that it is a cleaner/better design compared the JSON I'm taking in.
I suggest two approaches that are very explicit and easy to follow for the next developer looking at the code.
Two classes
creating a intermediate dto class that is used for deserialisation and then creating the business logic object from that intermediate object.
var withAttributes = Deserialise<ListingDto>();
var flatObject = new Listing(withAttributes);
One class
You could provide accessors at the top level which dip into the subclasses.
public class Listing
{
public AttributesDto Attributes {get; set}
...
public string Url => Attributes.Url; // Maybe '?.Url'
}
I am getting the below JSON in response from a REST API.
{
"data":{
"id":123,
"zoneid":"mydomain.com",
"parent_id":null,
"name":"jaz",
"content":"172.1 6.15.235",
"ttl":60,
"priority":null,
"type":"A",
"regions":[
"global"
],
"system_record":false,
"created_at":"2017-09-28T12:12:17Z",
"updated_at":"2017-09-28T12:12:17Z"
}
}
and trying to resolve using below code but that doesn't result in a correctly deserialized type.
var model = JsonConvert.DeserializeObject<ResponseModel>(response);
below is a class according the field I received in JSON response.
public class ResponseModel
{
public int id { get; set; }
public string zone_id { get; set; }
public int parent_id { get; set; }
public string name { get; set; }
public string content { get; set; }
public int ttl { get; set; }
public int priority { get; set; }
public string type { get; set; }
public string[] regions { get; set; }
public bool system_record { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}
What is missing?
You're missing a wrapper class.
public class Wrapper
{
public ResponseModel data {get;set}
}
and then do:
var model = JsonConvert.DeserializeObject<Wrapper>(response).data;
to get the instance of your ResponseModel out the data property.
You can deduct this from your json:
{ "data":
{ "id":123, /*rest omitted */ }
}
The type that will receive this JSON needs to have a property named data. The suggested Wrapper class acts as that type.
According to json2csharp website, your model seems to be incorrect. Try this one :
public class ResponseModel
{
public int id { get; set; }
public string zoneid { get; set; }
public object parent_id { get; set; }
public string name { get; set; }
public string content { get; set; }
public int ttl { get; set; }
public object priority { get; set; }
public string type { get; set; }
public List<string> regions { get; set; }
public bool system_record { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
}
public class RootObject
{
public ResponseModel data { get; set; }
}
Here a cool trick you can do in Visual Studio 2015-2017 where it generates the the correct class if you just copy the JSON (ctrl + c).
You need to create a new class in visual studio and once inside the class go to Edit menu -> Paste special -> paste JSON As Classes.
Steps to generate json class
This will generate the C# object for that json for you and save you all the hassle :)
Your model does not match your response - it matches the data property. Simply wrap another object round it
public class ResponseData
{
public ResponseModel Data {get; set; {
}
and then
var model = JsonConvert.DeserializeObject<ResponseData>(response);
I have data contract for WCF and its got custom class variable reference, I wondering do I need to do anything additional in configuration or anything else. i.e. AddressData
Member class
[DataContract]
public class MemberData : IIdentifiableEntity
{
[DataMember]
public int MemberID { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Surname { get; set; }
[DataMember]
public string Forename { get; set; }
[DataMember]
public string MiddleName { get; set; }
[DataMember]
public string PrevSurname { get; set; }
[DataMember]
public System.DateTime DOB { get; set; }
[DataMember]
public string Sex { get; set; }
[DataMember]
public AddressData Address { get; set; }
[DataMember]
public ContactDetailData ContactDetail { get; set; }
[DataMember]
public MembershipData Membership { get; set; }
int IIdentifiableEntity.EntityId
{
get { return MemberID; }
set { MemberID = value; }
}
}
Address class
public class AddressData
{
public int MemberID { get; set; }
public int AddressType { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string PostCode { get; set; }
public string City { get; set; }
public string County { get; set; }
public string Town { get; set; }
public string Country { get; set; }
public MemberData Member { get; set; }
}
do I need to do anything additional
While you don't need to use the DataMember attribute to decorate your service types (WCF will still serialize them, as long as they only contain serializable types), you should generally still use the attribute.
From https://stackoverflow.com/a/4836803/569662:
without [DataContract], you cannot define an XML namespace for your data to live in
without [DataMember], you cannot serialize non-public properties or fields
without [DataMember], you cannot define an order of serialization (Order=) and the DCS will serialize all properties alphabetically
without [DataMember], you cannot define a different name for your property (Name=)
without [DataMember], you cannot define things like IsRequired= or other useful attributes
without [DataMember], you cannot leave out certain public properties - all public properties will be serialized by the DCS
I want to convert string to json, but all I get is null or error
For example I want to convert this string:
string json =
"{\"$id\":\"1\",\"Result\":{\"$id\":\"2\",\"ListRate\":[{\"$id\":\"3\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":0.0},{\"$id\":\"4\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":0.0},{\"$id\":\"5\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":0.0}],\"MyRate\":[{\"$id\":\"6\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":1.5},{\"$id\":\"7\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":2.0},{\"$id\":\"8\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":2.0}],\"ListRated\":[{\"$id\":\"9\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":30.0,\"theCount\":1,\"theSum\":1.5},{\"$id\":\"10\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":40.0,\"theCount\":1,\"theSum\":2.0},{\"$id\":\"11\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":40.0,\"theCount\":1,\"theSum\":2.0}]},\"StatusCode\":\"Created\",\"Description\":null}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var myclass = Newtonsoft.Json.JsonConvert.DeserializeObject<ProductDto>(json);
And Class of product:
public class RootObject
{
public string id { get; set; }
public ProductDto Result { get; set; }
public string StatusCode { get; set; }
public object Description { get; set; }
}
public class ProductDto
{
public string id { get; set; }
public IQueryable<MyRateDto> ListRate { get; set; }
public IQueryable<MyRateDto> MyRate { get; set; }
public IQueryable<ShoppingRateDto> ListRated { get; set; }
}
public class ShoppingRateDto
{
public string id { get; set; }
public string Title { get; set; }
public long ShoppingRateId { get; set; }
public double Rate { get; set; }
public int theCount { get; set; }
public double theSum { get; set; }
}
public class MyRateDto
{
public string id { get; set; }
public string Title { get; set; }
public long ShoppingRateId { get; set; }
public double Rate { get; set; }
}
I'm a bit confused how should I convert it .
Also I have using "RootObject" instead of "ProductDto", but nothing changed ...
Some of error:
Additional information: The best overloaded method match for
'Newtonsoft.Json.JsonConvert.DeserializeObject(string)'
has some invalid arguments
Additional information: Cannot create and populate list type
System.Linq.IQueryable`1[Site.Model.MyRateDto]. Path 'Result.ListRate'
Seems like the error is a decent hint. Instead of using IQueryable (basically any interface collections), try using a concrete collection. e.g. List<T>
Serializers have to work with concrete classes to be able to instantiate them through reflection. When they are reading class definitions, and they come across interfaces, seldom can the serializer make a choice on the correct implementation. Since there are potentially many many concrete instances of different collection interfaces, you should provide the serializers with concrete types in order to work properly.
I'm using MVC Controller. Json method in order to send json result object to my javascript function.
From time to time I want to omit one of my object's property from the json result object.
How can I achive it?
This is my .NET object:
public class jsTreeModel
{
public string Data { get; set; }
public JsTreeAttribute Att { get; set; }
public string State { get; set; }
public List<jsTreeModel> Children { get; set; }
}
I this case I want to omit the 'Children' property from the json result.
Any idea?
If you are using Json.NET as your serializer, you can omit a property fairly simply:
public class jsTreeModel
{
public string Data { get; set; }
public JsTreeAttribute Att { get; set; }
public string State { get; set; }
[JsonIgnore]
public List<jsTreeModel> Children { get; set; }
}
I hope this helps you!