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.
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'
}
It's been a while since i've used automapper, but i'm almost sure that my situation should be possible.
Setup
I created the following mapping configuration:
var map = cfg.CreateMap<TSource, Structure>();
So in my situation the source is a generic type (unknown) and the target type is Structure (known).
A possible option for the TSource type could be:
public class DataChannel
{
public string Id { get; set; }
public string Description { get; set; }
public string Ean { get; set; }
public DateTimeOffset ValidFrom { get; set; }
public bool IsManual { get; set; }
public string Type { get; set; }
public string Unit { get; set; }
public string Address { get; set; }
public string BuildingId { get; set; }
}
The target Structure object looks like this:
public class Structure : IStructure
{
public Structure()
{
Children = new List<Structure>();
Properties = new List<StructureProperty>();
}
public int Id { get; set; }
public ICollection<StructureProperty> Properties { get; set; }
public List<Structure> Children { get; set; }
}
Situation
For example, I would like the string properties "Unit" and "Type" to be added as a StructureProperty object to the Properties collection of the Structure entity.
map.ForMember(c => c.Properties, m => m.MapFrom<StructurePropertyResolver<TSource>>());
How can this be done?
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 am currently receiving two different types of objects in Json format and I need to deserialize those Json into corresponding objects. However, my problem is that all of those objects are sent by the same channel which means when I receive an object but I don't know which type the object is. The two different types of objects is shown below. So my question is if there is a function that allows me to try to deserialize with one object type and if that fails, I use another one?
//Object1
public class OutputStream
{
public DateTime summaryId { get; set; }
public string total { get; set; }
public int userId { get; set; }
public string dataType { get; set; }
public SensorLocation location { get; set; }
public HeartRateActivityType activityType { get; set; }
}
//Object2
public class OutputStreamAMM
{
[DataMember]
public DateTime summaryId { get; set; }
[DataMember]
public string average { get; set; }
[DataMember]
public string max { get; set; }
[DataMember]
public string min { get; set; }
[DataMember]
public int userId { get; set; }
[DataMember]
public string dataType { get; set; }
[DataMember]
public SensorLocation location { get; set; }
[DataMember]
public HeartRateActivityType activityType { get; set; }
}
Any help is appreciated.
You could write a Json Schema for one of your models, and then validate your json document against the schema to see if it matches.
You can use Newtonsoft.Json for this.
var schema = JsonSchema.Parse(...);
var jtoken = JToken.Parse(jsonString);
if(jtoken.IsValid(schema))
{
var model = jtoken.ToObject<OutputStream>();
}
else
{
var model = jtoken.ToObject<OutputStreamAMM>();
}
A simpler way would be to check for a key field that only one of the classes is known to have:
var token = JToken.Parse(jsonString);
if(token.SelectToken("$.average") != null)
{
val model = token.ToObject<OutputStreamAMM>();
}
else
{
val model = token.ToObject<OutputStream>();
}
Can you just inspect the json for some type of key identifier?
For example if the json contains "average" you have OutputStreamAMM. If not you have OutputStream.
Write a function that has that logic.
This one really has me stumped. I'm trying to deserialize the following JSON string which I get from a ASP.NET Web Service:
"{\"d\":{\"__type\":\"KPCServer.LogonResult\",\"User\":{\"UserId\":\"affaa328-5b53-430e-991a-22674ede6faf\",\"Email\":\"test#test.com\",\"Alias\":\"Mike\",\"FullName\":\"Mike Christensen\",\"Password\":\"secret\",\"Location\":\"Redmond, WA\",\"ImageUrl\":null,\"DateOfBirth\":\"\\/Date(-62135568000000)\\/\",\"LastLogon\":\"\\/Date(1350450228000)\\/\",\"UserSince\":\"\\/Date(1197980020000)\\/\",\"MailingList\":true,\"Bio\":\"Test\"},\"NewUser\":false,\"Ticket\":\"FJEjfje87fjef88fe8FAF8fA88fAjk+AFJ9fja9Fa9Ff99aJF9aFjfA99fjaBFJ7zqmlcHn9Dfw=\"}}"
I have the following types:
public class User
{
public Guid UserId { get; set; }
public string Email { get; set; }
public string Alias { get; set; }
public string FullName { get; set; }
public string Password { get; set; }
public string Location { get; set; }
public string ImageUrl { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime LastLogon { get; set; }
public DateTime UserSince { get; set; }
public bool MailingList { get; set; }
public string Bio { get; set; }
}
[DataContract(Name="KPCServer.LogonResult")]
public class LogonResult
{
[DataMember] public User User { get; set; }
[DataMember] public bool NewUser { get; set; }
[DataMember] public string Ticket { get; set; }
}
[DataContract]
[KnownType(typeof(LogonResult))]
public class Result<T>
{
[DataMember]
public T d { get; set; }
}
I then try to deserialize the string using:
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Result<T>));
Result<T> result = serializer.ReadObject(stream) as Result<T>;
return result.d;
}
Note: In the above method, T is of type LogonResult.
However, I get the following exception on ReadObject:
System.Runtime.Serialization.SerializationException was unhandled by user code
HResult=-2146233076
Message=JSON contains a '__type' member specifying the data contract name ':KPCServer.LogonResult'. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'KPCServer.LogonResult' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer. You can also often eliminate this error by avoiding the use of derived types where the JSON is produced.
Source=System.ServiceModel.Web
InnerException:
If I run:
json = json.Replace("_type", "_blah");
Then everything works fine. This is using Silverlight on Windows Phone 8.
This is due to the fact that:
"\"__type\":\"KPCServer.LogonResult\""
doesn't contain a data contract namespace. This is fixed by modifying the DataContractAttribute on LogonResult:
[DataContract(Name = "KPCServer.LogonResult", Namespace="")]
public class LogonResult
{
[DataMember]
public User User { get; set; }
[DataMember]
public bool NewUser { get; set; }
[DataMember]
public string Ticket { get; set; }
}