You can still use built-in types, but you'll need to use DataContractJsonSerializer
instead of JavaScriptSerializer
, and add the appropriate DataContract
and DataMember
attributes - the implementation is a bit different, but still pretty straightforward.
One thing - your Sold
property is boolean, but your JSON sample has a string there - booleans are valid JSON types, so you can remove the quotes.
Some working code:
JSON:
[{"AdvertId":"1234567","Price Original":"500","Sold":false}]
C#:
var ser = new DataContractJsonSerializer(typeof(Sample[]));
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(JSON))) {
Sample[] s = (Sample[])ser.ReadObject(ms);
}
[DataContract]
public class Sample {
[DataMember]
public int AdvertId { get; set; }
[DataMember(Name = "Price Original")]
public string PriceOriginal { get; set; }
[DataMember]
public bool Sold { get; set; }
}