after deserialize this Json, I'm not able to bind the objects into class model.
[{
"products": {
"prd10": null,
"prd20": [
{
"pinno": "260158299582",
"expirydate": "2019-12-31",
"remark": "remark 1"
}
],
"prd30": [
{
"pinno": "782252223809",
"expirydate": "2019-12-31",
"remark": "remark 2"
},
{
"pinno": "875928008089",
"expirydate": "2019-12-31",
"remark": "remark 3"
}
],
"user key": "333573536fbfe5164",
"post date": "2019-07-24T17:43:56.888179+08:00"
}
}]
public class products
{
public List<prod_details> product{ get; set; }
}
public class prod_details
{
public string pinno { get; set; }
public string expirydate { get; set; }
public string remark { get; set; }
}
products myModel = JsonConvert.DeserializeObject(json_string);
I had tried with dynamic result, but still not able to retrieve the data. Please help!
Below is the solution of this json deserialization :
Note : I have modified your array of prd10, prd20 etc to common name like prd only otherwise it will return null model.
I have modified your Json Object like below :
[{
"products": {
"prd": null,
"prd": [
{
"pinno": "260158299582",
"expirydate": "2019-12-31",
"remark": "remark 1"
}
],
"prd": [
{
"pinno": "782252223809",
"expirydate": "2019-12-31",
"remark": "remark 2"
},
{
"pinno": "875928008089",
"expirydate": "2019-12-31",
"remark": "remark 3"
}
],
"user key": "333573536fbfe5164",
"post date": "2019-07-17"
}
}]
And you have to create model like below as per your Json Object :
public partial class ProductClass
{
[JsonProperty("products")]
public Products Products { get; set; }
}
public partial class Products
{
[JsonProperty("prd")]
public List<Prd> Prd { get; set; }
[JsonProperty("user key")]
public string UserKey { get; set; }
[JsonProperty("post date")]
public DateTimeOffset PostDate { get; set; }
}
public partial class Prd
{
[JsonProperty("pinno")]
public string Pinno { get; set; }
[JsonProperty("expirydate")]
public DateTimeOffset Expirydate { get; set; }
[JsonProperty("remark")]
public string Remark { get; set; }
}
Now you have to deserialize json object to class :
string JsonObj = "[{ products: { prd: null, prd: [ { pinno: 260158299582, expirydate: '2019-12-31', remark: 'remark 1' } ], prd: [ { pinno: 782252223809, expirydate: '2019-12-31', remark: 'remark 2' }, { pinno: 875928008089, expirydate: '2019-12-31', remark: 'remark 3' } ], 'user key': '333573536fbfe5164', 'post date': '2019-07-17' } }]";
List<ProductClass> result = JsonConvert.DeserializeObject<List<ProductClass>>(JsonObj.ToString());
Here is the snap of output :
Cheers !!