Deserialize a JSON array - c#

I am trying to deserialize JSON data in C# using Newtonsoft namespace.
Here are my classes:
class lastResponse
{
public string type { get; set; }
public Metadata metadata { get; set; }
// public string[] course { get; set; }
public List<object> course { get; set; }
public string publisher { get; set; }
}
public class Metadata
{
public string bookID { get; set; }
public string title { get; set; }
public string filename { get; set; }
}
This code:
var errorMsg = JsonConvert.DeserializeObject<lastResponse>(downloader.LastResponse);
Gives me this error:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BookManager.lastResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
Please help me figure out what I'm missing here.

The most possible reason for this could be the json you're getting from downloader.LastResponse is not an array.
Your Json could be like this.
{
"type": "First",
"metadata": {
"bookID": "book123",
"title": "bookTitle Here",
"filename": "book file name"
},
"course": [
{
"1": "01",
"2": "02"
},
{
"001": "001",
"002": "002"
}
],
"publisher": "you"
}
What you need to change is either pass JSON array in downloader.LastResponse or you can change your deserialize object statement as follows.
JsonConvert.DeserializeObject<lastResponse>(downloader.LastResponse);
According to what you're doing the correct JSON will be like this.
**[**
{
"type": "First",
"metadata": {
"bookID": "book123",
"title": "bookTitle Here",
"filename": "book file name"
},
"course": [
{
"1": "01",
"2": "02"
},
{
"001": "001",
"002": "002"
}
],
"publisher": "you"
}
**]**
This might solve your problem :)

you can use javascriptserializer
string s = "YouJsonText";
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(s);
//or
YouCustomClass res = serializer.Deserialize<YouCustomClass>(sb.ToString());
Also, you can use CustomJsonConverter like this:
public class YouCustomClassConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
//and first you need register type, which you want Deserialize
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(YouCustomClass ) }; }
}
}
//and then example of using JavaScriptSerializer with custom converter
var ser = new JavaScriptSerializer();
ser.RegisterConverters(new JavaScriptConverter[] { new YouCustomClassConverter() });
try
{
YouCustomClass obj = ser.Deserialize(jsonString);
}
Note: you need use using System.Web.Script.Serialization;

Related

How to deserialize Json into an C# object and fill the components into a dictionary

{
"success": true,
"data": [{
"type": "Employee",
"attributes": {
"id": {
"label": "ID",
"value": 8556527,
"type": "integer",
"universal_id": "id"
},
"email": {
"label": "Email",
"value": "exapmle#gmail.com",
"type": "standard",
"universal_id": "email"
},
"dynamic_2682839": {
"label": "DATEV Personalnumber",
"value": "31604",
"type": "standard",
"universal_id": "staff_number"
}
}
},
public class Id
{
[JsonPropertyName("label")]
public string Label { get; set; }
[JsonPropertyName("value")]
public int Value { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("universal_id")]
public string UniversalId { get; set; }
}
public class Email
{
[JsonPropertyName("label")]
public string Label { get; set; }
[JsonPropertyName("value")]
public string Value { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("universal_id")]
public string UniversalId { get; set; }
}
public class Dynamic2682839
{
[JsonPropertyName("label")]
public string Label { get; set; }
[JsonPropertyName("value")]
public string Value { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("universal_id")]
public string UniversalId { get; set; }
}
public class Attributes
{
[JsonPropertyName("id")]
public Id Id { get; set; }
[JsonPropertyName("email")]
public Email Email { get; set; }
[JsonPropertyName("dynamic_2682839")]
public Dynamic2682839 Dynamic2682839 { get; set; }
}
public class Datum
{
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("attributes")]
public Attributes Attributes { get; set; }
}
public class Root
{
[JsonPropertyName("success")]
public bool Success { get; set; }
[JsonPropertyName("data")]
public List<Datum> Data { get; set; }
}
I know it is probably are very simple solution behind it but i cant get to it. My Problem is that I want to deserialize this type of data into an object for example. This json file goes on forever but the only thing I'm interested in are the "ID" and the "Personalnumber" so I can create a dictionary for further processing with my code.
I mean I could just sit there for two days and add everything to the dictionary but first of all it would drive me crazy and second of all the dictionary should add new members automatically to the dictionary and shouldn't be static. I already converted the Json into class objects and downloaded Newtonsoft for my IDE and with this comand:
Id ids = JsonConvert.DeserializeObject<Id>(json);
Console.WriteLine(ids.Value);
I try to get all the values from the ID´s but all i get is a 0.
The thing is i tested everything so far and it works perfectly but my deserialization dont work as planned.
If anyone could help a newbie I would appreciate it.
This looks wrong
Id ids = JsonConvert.DeserializeObject<Id>(json);
You should probably be deserializing to Root instead.
Root root = JsonConvert.DeserializeObject<Root>(json);
From there you can get all the IDs with
List<Id> ids = root.Data.Select(datum => datum.Attributes.Id);
This json file goes on forever but the only thing I'm interested in are the "ID" and the "Personalnumber" ... the dictionary should add new members automatically ... and shouldn't be static.
Phuzi's answer seizes onto the wrong thing, giving you the contents of the "id" object, which does get you the ID, but ignores the Personalnumber..
..which also looks like it might not always be in a dynamic_2682839
We need a different strategy for getting the data; we can look to see if the label contains ID or Personalnumber but we could really do with the serializes deser'ing this data to a common set of objects, not a different Type (with the same properties) each time
I don't know if you used QuickType.IO for generating your JSON classes but, if you didn't, I would recommend it; it's a bit more sophisticated than other generators. You can trick it into helping you more by modifying your JSON..
Take this:
{
"success": true,
"data": [
{
"type": "Employee",
"attributes": {
"id": {
"label": "ID",
"value": 8556527,
"type": "integer",
"universal_id": "id"
},
"email": {
"label": "Email",
"value": "exapmle#gmail.com",
"type": "standard",
"universal_id": "email"
},
"dynamic_2682839": {
"label": "DATEV Personalnumber",
"value": "31604",
"type": "standard",
"universal_id": "staff_number"
}
}
}
]
}
And make it into this (put some more objects in "attributes", and make the keys of the dictionary into sequential numbers):
{
"success": true,
"data": [{
"type": "Employee",
"attributes": {
"1": {
"label": "ID",
"value": 8556527,
"type": "integer",
"universal_id": "id"
},
"2": {
"label": "Email",
"value": "exapmle#gmail.com",
"type": "standard",
"universal_id": "email"
},
"3": {
"label": "DATEV Personalnumber",
"value": "31604",
"type": "standard",
"universal_id": "staff_number"
},
"4": {
"label": "DATEV Personalnumber",
"value": "31604",
"type": "standard",
"universal_id": "staff_number"
},
"5": {
"label": "DATEV Personalnumber",
"value": "31604",
"type": "standard",
"universal_id": "staff_number"
}
}
}]
}
which will help QT see more easily that a Dictionary<string, Attribute> is suitable for "attributes", and a custom converter is needed for the varying type of "value"...
Otherwise you'll just get multiple identical classes (your Id, Email and Dynamic_123 classes) and a property for every member of "attributes" even though they're all the same:
You thus get these classes out of QT:
namespace SomeNamespace
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class SomeRoot
{
[JsonProperty("success")]
public bool Success { get; set; }
[JsonProperty("data")]
public Datum[] Data { get; set; }
}
public partial class Datum
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("attributes")]
public Dictionary<string, Attribute> Attributes { get; set; }
}
public partial class Attribute
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("value")]
public Value Value { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("universal_id")]
public string UniversalId { get; set; }
}
public partial struct Value
{
public long? Integer;
public string String;
public static implicit operator Value(long Integer) => new Value { Integer = Integer };
public static implicit operator Value(string String) => new Value { String = String };
}
public partial class SomeRoot
{
public static SomeRoot FromJson(string json) => JsonConvert.DeserializeObject<SomeRoot>(json, SomeNamespace.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this SomeRoot self) => JsonConvert.SerializeObject(self, SomeNamespace.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
ValueConverter.Singleton,
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ValueConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(Value) || t == typeof(Value?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.Integer:
var integerValue = serializer.Deserialize<long>(reader);
return new Value { Integer = integerValue };
case JsonToken.String:
case JsonToken.Date:
var stringValue = serializer.Deserialize<string>(reader);
return new Value { String = stringValue };
}
throw new Exception("Cannot unmarshal type Value");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (Value)untypedValue;
if (value.Integer != null)
{
serializer.Serialize(writer, value.Integer.Value);
return;
}
if (value.String != null)
{
serializer.Serialize(writer, value.String);
return;
}
throw new Exception("Cannot marshal type Value");
}
public static readonly ValueConverter Singleton = new ValueConverter();
}
}
and you can get your ID/Personalnumber attributes like this:
var someRoot = SomeRoot.FromJson(File.ReadAllText("a.json"));
var attribs = someRoot.Data.SelectMany(d => d.Attributes.Where(a => a.Value.Label == "ID" || a.Value.Label.Contains("Personalnumber"))).ToArray();
or some other LINQ of your choosing on the Dictionary at the path "data"."attributes" - here I've chosen to SelectMany; if Data had 10 elements, and each element had 4 Attributes, 2 of which were ID or Personalnumber, you'd get a single array of 20 long, of the ID/Personalnumber Attributes. You might not want to SelectMany, if you want to keep them together (they're only loosely related by ordering in the array after a SelectMany)

C# Deserializing JSON of an array, of single-value and multi-value results

I am trying to deserialize a structure that contains one item, with multiple items in it, that in turn potentially contain single or multiple items inside it... Here is the example of JSON:
"Content": {
"title": {
"$type": "EntityModelData",
"Id": "1133",
"Content": {
"intro": "Example Introduction",
"title": "Example Title",
}
},
"sections": {
"$type": "ContentModelData[]",
"$values": [
{
"title": {
"$type": "EntityModelData",
"Id": "1232",
"Content": {
"title": "Section 1 (Single-value context)"
}
},
"context": {
"$type": "ContentModelData",
"publicationType": {
"$type": "KeywordModelData",
"Id": "1175",
}
}
},
{
"title": {
"$type": "EntityModelData",
"Id": "1234",
"Content": {
"title": "Section 2 (Multi-valued context)"
}
},
"context": {
"$type": "ContentModelData[]",
"$values": [
{
"publicationType": {
"$type": "KeywordModelData",
"Id": "1182",
},
"contentType": {
"$type": "KeywordModelData",
"Id": "1166",
}
},
{
"publicationType": {
"$type": "KeywordModelData",
"Id": "1182",
},
"contentType": {
"$type": "KeywordModelData",
"Id": "1238",
}
}
]
}
}
]
}
}
I have set up my classes as follows...
public class ContentModel
{
[JsonProperty("Content")]
public Content Content { get; set; }
}
public class Content
{
[JsonProperty("title")]
public TitleModel Title { get; set; }
[JsonProperty("sections")]
public List<Sections> Sections { get; set; }
}
//[JsonArrayAttribute("sections")] (Putting this in makes it fail!)
public class Sections
{
[JsonProperty("title")]
public TitleModel Title { get; set; }
//[JsonProperty("context")]
//public Context Context { get; set; }
//[JsonProperty("context")]
//public IList<Context> Contexts { get; set; }
}
public class Context
{
//[JsonProperty("$type")]
//public string Type { get; set; }
//[JsonProperty("publicationType")]
//public Keyword PublicationType { get; set; }
//[JsonProperty("contentType")]
//public Keyword ContentType { get; set; }
//[JsonProperty("subject")]
//public Keyword Subject { get; set; }
}
Note, some of it is commented out, as it breaks when I include or try further deserialize the context-items (single/multi)
The deserialize command I am using is...
var pageContent = JObject.Parse([[above-content-that has a lot more in it]]);
foreach (var entity in pageContent.SelectToken(".Parent[0].ContanerForContent[[which has the above JSON it it]]"))
{
var content = entity.ToObject<ContentModel>().Content; break;
}
Now, this is currently successful at deserializing up to the title!!! (with the comments that I have above in the code) ... I can loop through a list of "sections" and get into their "Title" ... but when I try to include the context (which is both single-valued and multi-valued... it throws the error below...
1.
List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'Regions[0].Entities[1].Content.sections.$values[2].context.$values', line 1, position 5038.'
2.
List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'Regions[0].Entities[1].Content.sections.$values', line 1, position 4214.'
3.
Newtonsoft.Json.JsonSerializationException:
'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Context]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type
(e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Regions[0].Entities[1].Content.guidebookSections.$values[0].context.publicationType', line 1, position 4271.'
I have tried a few different ways, but keep getting variations of the error at different levels.
At this point - I think I am going about this the wrong way completely. How do I deserialize the "context" items?
Thanks for the inputs above... all of them were educational, and I eventually found one that worked for me...
public class SingleValueArrayConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo["$type"].Value<string>().Contains("[]"))
{
return jo.ToObject<List<T>>(serializer);
}
else
{
return new List<T>(new[] { jo.ToObject<T>(serializer) });
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
and then on my properties where I could get either a list or a single item... I add this:
[JsonProperty("context")]
[JsonConverter(typeof(SingleValueArrayConverter<Context>))]
public List<Context> Context { get; set; }

JsonConverter byte[]

I have the following code:
//Models
public class Home{
public Guid HomeId { get; set; }
public Guid UserId { get; set; }
public string Name { get; set; }
public byte[] Description1 { get; set; }
public virtual User User { get; set; }
}
public class User
{
public Guid UserId { get; set; }
public string Email { get; set; }
public virtual Perfil Perfil { get; set; }
}
public class Perfil
{
public Guid UserId { get; private set; }
public string Name { get; private set; }
public byte[] Description2 { get; set; }
}
// JsonConverter
public class ByteArrayConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(byte[]);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var m = Convert.FromBase64String((string)reader.Value);
return m;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
byte[] bytes = (byte[])value;
writer.WriteValue(Convert.ToBase64String(bytes));
}
}
// Serialize (Calls the WriteJson function of the ByteArrayConverter, OK)
var serialized = JsonConvert.SerializeObject(obj, settings);
{
"HomeId": "2925362b",
"UserId": "9ea43c30",
"Name": "Name 1",
"Description1": "VABlAHMAdABlAA==", //===> Converts to Base64 (OK)
"User": {
"UserId": "9ea43c30",
"Email": "aaaa#aaaa.com",
"Perfil": {
"UserId": "9ea43c30",
"Name": "Name 2",
"Description2": "dABlAHMAdABlAA==", //===> Converts to Base64 (OK)
}
}
}
// Deserialize
var deserialized = JsonConvert.DeserializeObject<T>(serialized, settings); // T is class Home
{
"HomeId": "2925362b",
"UserId": "9ea43c30",
"Name": "Name 1",
"Description1": "VABlAHMAdABlAA==", //===> Don't Convert from Base64, (**Does not call the ReadJson function of ByteArrayConverter**)
"User": {
"UserId": "9ea43c30",
"Email": "aaaa#aaaa.com",
"Perfil": {
"UserId": "9ea43c30",
"Name": "Name 2",
"Description2": "dABlAHMAdABlAA==", //===> Convert from Base64 (OK)
}
}
}
Settings
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
};
settings.Converters.Add(new ByteArrayConverter());
The Description1 property does not get into the ReadJson function of the ByteArrayConverter class, so it does not convert back the byte [], generating another invalid [] byte, ....
Any idea of this problem?
Old question, but need clarification
when I do not use as above, it serializes a byte[] type with the information: $ type: "System.Byte [], mscorlib" $ Value: "VABlAHMAdABlAA ==", but when I go deserialize it shows an error: Unexpected character encountered while parsing value
To avoid the above error in deserialization, use the following json setting in deserialization.
TypeNameHandling = TypeNameHandling.None
TypeNameHandling setting include type information when serializing JSON and read type information so that the create types are created when deserializing JSON
For more details read: TypeNameHandling Enumeration
The serialization of byte[] is Base-64 string, and when deserialized it generate byte[] again.
You need not ByteArrayConverter as given in the comments of #Brian Rogers and #dbc
You can find a complete working example using your code with modification online

Datacontract object and string

I'm trying to parse some results from a web service in C# visual studio 2010 that are coming back in JSON but the field named data is sometime a string and sometimes an object. I'm not sure how to get the data contracts to handle that. When it runs through the deserialization it just returns a null result for the everything. If i try a web request that doesn't have any profile_results then works OK.
The JSON looks like this
{
"ListSingleOrganization": {
"id": 86270,
"short_name": "someshort name",
"long_name": "Long name",
"created_at": "2014-05-02T14:21:06Z",
"is_disabled": false,
"description": "Verbose description",
"renewed_at": "2014-05-02T14:21:07Z",
"alternate_id": null,
"website_url": "http://www.url.com",
"member_count": 50,
"pic_url": "https://www.somepicture.com/1.jpg",
"umbrella_id": 36016,
"keywords": "Fraternity, Leadership, Human Service, Scholarship",
"category": {
"id": 53282,
"name": "Fraternities"
},
"profile_responses": [
{
"element": {
"id": 51350,
"name": "Group Email Address",
"type": "Email"
},
"data": "groupsemail#gmail.com"
},
{
"element": {
"id": 1239634,
"name": "Please list your organization's Twitter handle so we may follow you with our office's account. (#something)",
"type": "TextField"
},
"data": "#handle"
},
{
"element": {
"id": 1192652,
"name": "Is this a new organization?",
"type": "Radio"
},
"data": {
"id": 2003570,
"name": "No"
}
}
]
}
}
the difference is based on the element type but i don't know how to account for that with them having the same name.
right now i have
[DataContract]
class ListSingleOrganization
{
//other members
[DataMember(Name = "profile_responses")]
public List<profile_responses> profile_responses { get; set; }
}
[DataContract]
class profile_responses
{
[DataMember(Name = "element")]
public element element { get; set; }
[DataMember(Name="data")]
public data data {get; set; }
}
[DataContract]
class element
{
[DataMember(Name = "id")]
public int id { get; set; }
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "type")]
public string type { get; set; }
}
[DataContract]
class data
{
[DataMember(Order=1)]
public string raw { get; set; }
[DataMember(Name = "file_name")]
public string file_name { get; set; }
[DataMember(Name = "url")]
public string url { get; set; }
[DataMember(Name = "id")]
public int id { get; set; }
[DataMember(Name = "name")]
public string name { get; set; }
}
You can create a custom formatter to read the property, check if it a string. If it's a string then create a new instance of your data class and set one of the properties to the string value. Otherwise, deserialize it to a your data object.
public class JsonDataConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(data));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
data returnVal = null;
// You may need to debug this and see which token type is being returned
// per data element and adjust, but the principle stands.
if (reader.TokenType == JsonToken.String)
{
returnVal = new data();
returnVal.raw = reader.ReadAsString();
}
else
{
returnVal = serializer.Deserialize<data>(reader);
}
return returnVal;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Assuming you only need to deserialize
throw new NotImplementedException();
}
}
Associate the JsonConverter with your property:
[DataMember(Name = "data")]
[JsonConverter(typeof(JsonDataConverter))]
public data data { get; set; }
EDIT: (answering follow up question)
Set this up in an isolated unit test and get it to work there first before trying to put this in a SSIS package. If you are using the DataContractSerializer then the JsonDataConverter class will not be invoked. You want to deserialize using Json.NET:
using Newtonsoft.Json;
. . .
var orgList JsonConvert.DeserializeObject<ListSingleOrganization>(webServiceResultString);
Couldn't get the DataContract Serializes to work with these irregularities so I went the dynamic object route and that worked.
Deserialize JSON into C# dynamic object?

How to deserialize JSON to my custom Class

Im new to JSON (and not sure if its the right way to do that), my problem is to deserialize my classes, all models implements this interface:
public interface IPersistent
{
object Id { get; set; }
}
Example of class:
public class ModelTest : IPersistent
{
private int? _id;
public object Id
{
get { return this._id; }
set { this._id = (int?)value; }
}
public string Name { get; set; }
}
Serialize method:
public void SerializeData<T>(T[] data)
{
var settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
DateFormatHandling = DateFormatHandling.IsoDateFormat
};
var result = JsonConvert.SerializeObject(data, Formatting.Indented, settings);
//more things happen, but not affect serialized data.
}
Deserialize method:
public T[] DeserializeData<T>(string objCached)
{
var settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
DateFormatHandling = DateFormatHandling.IsoDateFormat
}; //not sure if a need this settings, but...
T[] result = JsonConvert.DeserializeObject<T[]>(objCached, settings); //error here.
return result;
}
Error:
Message=Specified cast is not valid.
objCached data:
[
{
"$id": "1",
"Id": 1000,
"Name": "Name 1"
},
{
"$id": "2",
"Id": 2000,
"Name": "Name 2"
},
{
"$id": "3",
"Id": 3000,
"Name": "Name 3"
},
{
"$id": "4",
"Id": 4000,
"Name": "Name 4"
}
]
I tried validate JSON result using:
http://json2csharp.com/
Result:
public class RootObject
{
public string __invalid_name__$id { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
I'm looking for something that changes just the methods (Serialize and Deseriaize), can't change ALL my models (its a legacy without any unit test).
All you need to do is changing the set method of Id property. (Because value is long when read from json, which can not be casted toint?)
public class ModelTest : IPersistent
{
private int? _id;
public object Id
{
get { return this._id; }
set { this._id = new Nullable<int>((int)(long)value); }
}
public string Name { get; set; }
}

Categories