如何解决如何使用字典对象或列表使用 Json C# 创建 Dto
我希望使用以下 JSON 创建一个 Dto 类我面临一个没有硬代码根编号的问题:“1”和“2”作为 JsonPropertyNames 它不起作用,我使用 Dictionary 对象来收集数据,Dto 类:StorageData_v1 必须使用像被引用/使用的字典对象来删除硬编码的 PropertyNam 值:
**I want to eliminate the below hardcoded values and collect them into Dictionary object like was used property: public Dictionary<string,StorageData> Data { get; set; }**
**Note: I have tried using JsonPropertyTagNames =new Object[]{"1",2"} as well,but it didn't work**
/// <summary>
/// DBK Data Details
/// </summary>
[JsonProperty(PropertyName = "2")]
public DataInfo DataDetails2 { get; set; }
/// <summary>
/// DBK Data Details
/// </summary>
[JsonProperty(PropertyName = "1")]
public DataInfo DataDetails { get; set; }
JSON string:
{
"apiVersion": 1,"data": {
"738": {
"1": {
"juris": "test","entity": "11","storage": {
"$podDbkRawBase64": "test"
},"instance": "1","calcJobId": "13d1c32252f24795","formIndex": "10","ReturnId": "b93c4acaa0c0"
}
},"463": {
"1": {
"juris": "test","entity": "test","storage": {
"$podRawBase64": "test2"
},"calcJobId": "13d1c32252f","formIndex": "4","ReturnId": "b93c4aca"
}
}
}
}
**Dto class:**
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace TestModels.Dto
{
/// <summary>
/// Unit data object
/// </summary>
public class StorageData_v1
{
/// <summary>
/// Input Screen Unit
/// </summary>
[JsonProperty(PropertyName = "apiVersion")]
public string ApiVersion { get; set; }
/// <summary>
/// Input Screen fields
/// </summary>
[JsonProperty(PropertyName = "data")]
public Dictionary<string,StorageData> Data { get; set; }
}
/// <summary>
/// DBK storage Information for DTO
/// </summary>
public class StorageData
{
/// <summary>
/// DBK Data Details
/// </summary>
[JsonProperty(PropertyName = "1")]
public DataInfo DataDetails { get; set; }
/// <summary>
/// DBK Data Details
/// </summary>
[JsonProperty(PropertyName = "2")]
public DataInfo DataDetails2 { get; set; }
}
public class DataInfo
{
/// <summary>
/// Jurisditction
/// </summary>
[JsonProperty(PropertyName = "juris")]
public string Jurisdiction { get; set; }
/// <summary>
/// DBK Entity type
/// </summary>
[JsonProperty(PropertyName = "entity")]
public string Entity { get; set; }
/// <summary>
/// DBK data Instance
/// </summary>
[JsonProperty(PropertyName = "instance")]
public string Instance { get; set; }
/// <summary>
/// DBK data Calc JobId
/// </summary>
[JsonProperty(PropertyName = "calcJobId")]
public string CalcJobId { get; set; }
/// <summary>
/// DBK data Index
/// </summary>
[JsonProperty(PropertyName = "formIndex")]
public string FormIndex { get; set; }
/// <summary>
/// Return Id
/// </summary>
[JsonProperty(PropertyName = "ReturnId")]
public string ReturnId { get; set; }
/// <summary>
/// Data fields
/// </summary>
[JsonProperty(PropertyName = "storage")]
public Storage Storage { get; set; }
}
public class Storage
{
/// <summary>
/// Base 64 data
/// </summary>
[JsonProperty(PropertyName = "$podRawBase64")]
public string PodRawBase64 { get; set; }
}
}
解决方法
您可以使用:
public class Storage
{
[JsonProperty("$podDbkRawBase64")]
public string PodDbkRawBase64 { get; set; }
[JsonProperty("$podRawBase64")]
public string PodRawBase64 { get; set; }
}
public class Root
{
public int apiVersion { get; set; }
[JsonProperty(PropertyName = "data")]
public Dictionary<string,Dictionary<string,DataItem>> DataItems { get; set; }
}
public class DataItem
{
public string juris { get; set; }
public string entity { get; set; }
public string instance { get; set; }
public string calcJobId { get; set; }
public string fromIndex { get; set; }
public string ReturnId { get; set; }
public Storage storage { get; set; }
}
你可以像这样反序列化你的对象:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。