如何使用 Json C# 使用字典对象或列表创建 Dto
How to create Dto using Json C# using Dictionary object or Lists
我正在寻找使用以下内容创建 Dto class JSON 我面临没有硬代码根号的问题:“1”和“2”作为 JsonPropertyNames 它不起作用,我正在使用用于收集数据的字典对象,Dto class: StorageData_v1 必须像 referered/used :
一样使用字典对象对 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"
},
"instance": "1",
"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);
我正在寻找使用以下内容创建 Dto class JSON 我面临没有硬代码根号的问题:“1”和“2”作为 JsonPropertyNames 它不起作用,我正在使用用于收集数据的字典对象,Dto class: StorageData_v1 必须像 referered/used :
一样使用字典对象对 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"
},
"instance": "1",
"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);