2

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!

3
  • 1
    your classes do not match this JSON Commented Jul 24, 2019 at 10:53
  • 1
    prd20,prd30 is string in prod_details class, but in json is Array Commented Jul 24, 2019 at 10:55
  • 1
    the class is an object not array and some values from the json object also you should specify the type in JsonConvert.DeserializeObject<products>(json_string); In order this to work Remove the [] on the root of the json object Commented Jul 24, 2019 at 11:03

1 Answer 1

3

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 :

enter image description here

Cheers !!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.