【问题标题】:Couldn't bind deserialize Json objects into class model无法将反序列化 Json 对象绑定到类模型中
【发布时间】:2019-07-24 10:48:13
【问题描述】:

反序列化此 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; }
}

产品 myModel = JsonConvert.DeserializeObject(json_string);

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

【问题讨论】:

  • 您的课程与此 JSON 不匹配
  • prd20,prd30prod_details 类中是string,但在json 中是Array
  • 该类是一个对象而不是数组,并且来自 json 对象的一些值也应该在 JsonConvert.DeserializeObject(json_string); 中指定类型为了使它起作用,请删除 json 对象根目录上的 []

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


【解决方案1】:

下面是这个json反序列化的解决方案:

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

我已经修改了您的 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 对象创建如下模型

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

这是输出的快照

干杯 !!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2021-10-29
    • 2016-12-30
    相关资源
    最近更新 更多