英文:
How to create Model Class of Json Object - c#
问题
以下是您要求的代码部分的翻译:
string serializedObj = JsonConvert.SerializeObject(nftList);
var rootObj = JsonConvert.DeserializeObject<List<Root>>(serializedObj);
如果您需要任何其他帮助,请随时告诉我。
英文:
I have a Json object to which I want to bind to a model class. I have tried different ways of creating a class but the mapping deos not work and rootObj
always return null properties, I think I am unable to generate a model according to the JSON string.
I am sharing two line code:
string serializedObj = JsonConvert.SerializeObject(nftList);
var rootObj = JsonConvert.DeserializeObject<List<Root>>(serializedObj);
My json string serializedObj
is:
[
{
"json": "{\"Listing\": {\"Period\": 1209600, \"AssetID\": \"536d6f6f74685965746934333130\", \"Interest\": 1000, \"PolicyID\": \"eaa972045049185981aca9f4aaad38bc307776c593e4a849d3802a87\", \"Principal\": 300000000, \"BorrowerPKH\": \"ab77bec82618a2a5e8d891ad03361186a625861227605cb65f8ff312\"}, \"Deadline\": 1678360215, \"LenderPKH\": \"0b03d391a4746acf14794e4eaa531b67c25d69abed8512d0ecf35534\"}",
"Principal": null
},
{
"json": "{\"Listing\": {\"Period\": 1814400, \"AssetID\": \"424a533030393039\", \"Interest\": 600, \"PolicyID\": \"e282271ec9251ba23fb123b0f53618b35cf5a6cde4170c003a0ebf13\", \"Principal\": 70000000, \"BorrowerPKH\": \"155452c3bb66efa0e8493a018a2a16cdc7a5aaed1ae04020a2ef8dce\"}, \"Deadline\": 1679012316, \"LenderPKH\": \"e838596e09bee22eeb8615f6fc377582bab16ff9d961c6f74338c8ea\"}",
"Principal": null
}
]
I am trying Newton json
答案1
得分: 1
以下是翻译好的内容:
问题在于你的JSON包含嵌套的JSON。你的实际JSON的一个合理模型如下:
public class Root
{
[JsonProperty("json")]
public string Json { get; set; }
// TODO: 可能需要使用不同的类型;我们无法确定它的含义
[JsonProperty("Principal")]
public string Principal { get; set; }
}
然后,你可能有另一个类,其中包含Listing
(它似乎需要另一个包含Period
的类等)。我现在将其称为UsefulRoot
(理想情况下,你可以给它一个更有意义的名称)。
要将原始JSON转换为List<UsefulRoot>
,你需要进行两阶段反序列化 - 一次用于顶层表示的包含JSON的JSON对象,然后一次用于它们各自的Json
属性:
var roots = JsonConvert.DeserializeObject<List<Root>>(serializedObj);
var usefulRoots = roots
.Select(root => JsonConvert.DeserializeObject<UsefulRoot>(root.Json))
.ToList();
英文:
The problem is that your JSON contains nested JSON. A reasonable model of your actual JSON is:
public class Root
{
[JsonProperty("json")]
public string Json { get; set; }
// TODO: Possibly use a different type; we can't tell what
// it's meant to be
[JsonProperty("Principal")]
public string Principal { get; set; }
}
You might then have a different class with Listing
(which looks like it needs another class containing Period
) etc. I'll call this UsefulRoot
for now. (Ideally you'd give it a more meaningful name.)
To get from your original JSON to a List<UsefulRoot>
you'd need to deserialize in two phases - once for the JSON objects represented at the top level, containing JSON, and then once for the Json
property of each of them:
var roots = JsonConvert.DeserializeObject<List<Root>>(serializedObj);
var usefulRoots = roots
.Select(root => JsonConvert.DeserializeObject<UsefulRoot>(root.Json))
.ToList();
答案2
得分: 0
你的 JSON 中包含另一个嵌套的 JSON。因此,你需要进行两次解析,首先解析你的 JSON,然后解析嵌套的 JSON。
List<Data> data = JArray.Parse(json)
.Select(ja => new Data {
Principal = (string)ja["Principal"],
Listing = JObject.Parse((string)ja["json"]).ToObject<ListingItem>()
}).ToList();
类:
public class Data
{
public ListingItem Listing { get; set; }
public string Principal { get; set; }
}
public class ListingItem
{
public Listing Listing { get; set; }
public int Deadline { get; set; }
public string LenderPKH { get; set; }
}
public class Listing
{
public int Period { get; set; }
public string AssetID { get; set; }
public int Interest { get; set; }
public string PolicyID { get; set; }
public int Principal { get; set; }
public string BorrowerPKH { get; set; }
}
英文:
You have another nested json inside of your json. So you will have to parse it twice, at first your json, after this nested json
List<Data> data = JArray.Parse(json)
.Select(ja => new Data {
Principal = (string)ja["Principal"],
Listing = JObject.Parse((string)ja["json"]).ToObject<ListingItem>()
}).ToList();
classes
public class Data
{
public ListingItem Listing { get; set; }
public string Principal { get; set; }
}
public class ListingItem
{
public Listing Listing { get; set; }
public int Deadline { get; set; }
public string LenderPKH { get; set; }
}
public class Listing
{
public int Period { get; set; }
public string AssetID { get; set; }
public int Interest { get; set; }
public string PolicyID { get; set; }
public int Principal { get; set; }
public string BorrowerPKH { get; set; }
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论