无法将反序列化的Json对象绑定到类模型中

时间:2019-07-24 10:48:13

标签: c# json asp.net-mvc deserialization json-deserialization

反序列化此Json之后,我无法将对象绑定到类模型中。

[{
    "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);

我曾尝试使用动态结果,但仍然无法检索数据。 请帮忙!

1 个答案:

答案 0 :(得分:1)

以下是此json反序列化的解决方案:

注意 我已将您的 prd10,prd20 等数组修改为通用名称,例如 prd 否则它将返回空模型。

我已如下所示修改了您的Json对象

[{
"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"
}
}]

并且您必须根据您的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; }
    }

现在您必须将json对象反序列化为类

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());

这是输出的快照

enter image description here

干杯 !!