如何创建 JSON 对象的模型类 – C#

huangapple go评论95阅读模式
英文:

How to create Model Class of Json Object - c#

问题

以下是您要求的代码部分的翻译:

  1. string serializedObj = JsonConvert.SerializeObject(nftList);
  2. 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:

  1. string serializedObj = JsonConvert.SerializeObject(nftList);
  2. var rootObj = JsonConvert.DeserializeObject&lt;List&lt;Root&gt;&gt;(serializedObj);

My json string serializedObj is:

  1. [
  2. {
  3. &quot;json&quot;: &quot;{\&quot;Listing\&quot;: {\&quot;Period\&quot;: 1209600, \&quot;AssetID\&quot;: \&quot;536d6f6f74685965746934333130\&quot;, \&quot;Interest\&quot;: 1000, \&quot;PolicyID\&quot;: \&quot;eaa972045049185981aca9f4aaad38bc307776c593e4a849d3802a87\&quot;, \&quot;Principal\&quot;: 300000000, \&quot;BorrowerPKH\&quot;: \&quot;ab77bec82618a2a5e8d891ad03361186a625861227605cb65f8ff312\&quot;}, \&quot;Deadline\&quot;: 1678360215, \&quot;LenderPKH\&quot;: \&quot;0b03d391a4746acf14794e4eaa531b67c25d69abed8512d0ecf35534\&quot;}&quot;,
  4. &quot;Principal&quot;: null
  5. },
  6. {
  7. &quot;json&quot;: &quot;{\&quot;Listing\&quot;: {\&quot;Period\&quot;: 1814400, \&quot;AssetID\&quot;: \&quot;424a533030393039\&quot;, \&quot;Interest\&quot;: 600, \&quot;PolicyID\&quot;: \&quot;e282271ec9251ba23fb123b0f53618b35cf5a6cde4170c003a0ebf13\&quot;, \&quot;Principal\&quot;: 70000000, \&quot;BorrowerPKH\&quot;: \&quot;155452c3bb66efa0e8493a018a2a16cdc7a5aaed1ae04020a2ef8dce\&quot;}, \&quot;Deadline\&quot;: 1679012316, \&quot;LenderPKH\&quot;: \&quot;e838596e09bee22eeb8615f6fc377582bab16ff9d961c6f74338c8ea\&quot;}&quot;,
  8. &quot;Principal&quot;: null
  9. }
  10. ]

I am trying Newton json

答案1

得分: 1

以下是翻译好的内容:

问题在于你的JSON包含嵌套的JSON。你的实际JSON的一个合理模型如下:

  1. public class Root
  2. {
  3. [JsonProperty("json")]
  4. public string Json { get; set; }
  5. // TODO: 可能需要使用不同的类型;我们无法确定它的含义
  6. [JsonProperty("Principal")]
  7. public string Principal { get; set; }
  8. }

然后,你可能有另一个类,其中包含Listing(它似乎需要另一个包含Period的类等)。我现在将其称为UsefulRoot(理想情况下,你可以给它一个更有意义的名称)。

要将原始JSON转换为List<UsefulRoot>,你需要进行两阶段反序列化 - 一次用于顶层表示的包含JSON的JSON对象,然后一次用于它们各自的Json属性:

  1. var roots = JsonConvert.DeserializeObject<List<Root>>(serializedObj);
  2. var usefulRoots = roots
  3. .Select(root => JsonConvert.DeserializeObject<UsefulRoot>(root.Json))
  4. .ToList();
英文:

The problem is that your JSON contains nested JSON. A reasonable model of your actual JSON is:

  1. public class Root
  2. {
  3. [JsonProperty(&quot;json&quot;)]
  4. public string Json { get; set; }
  5. // TODO: Possibly use a different type; we can&#39;t tell what
  6. // it&#39;s meant to be
  7. [JsonProperty(&quot;Principal&quot;)]
  8. public string Principal { get; set; }
  9. }

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&lt;UsefulRoot&gt; 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:

  1. var roots = JsonConvert.DeserializeObject&lt;List&lt;Root&gt;&gt;(serializedObj);
  2. var usefulRoots = roots
  3. .Select(root =&gt; JsonConvert.DeserializeObject&lt;UsefulRoot&gt;(root.Json))
  4. .ToList();

答案2

得分: 0

你的 JSON 中包含另一个嵌套的 JSON。因此,你需要进行两次解析,首先解析你的 JSON,然后解析嵌套的 JSON。

  1. List<Data> data = JArray.Parse(json)
  2. .Select(ja => new Data {
  3. Principal = (string)ja["Principal"],
  4. Listing = JObject.Parse((string)ja["json"]).ToObject<ListingItem>()
  5. }).ToList();

类:

  1. public class Data
  2. {
  3. public ListingItem Listing { get; set; }
  4. public string Principal { get; set; }
  5. }
  6. public class ListingItem
  7. {
  8. public Listing Listing { get; set; }
  9. public int Deadline { get; set; }
  10. public string LenderPKH { get; set; }
  11. }
  12. public class Listing
  13. {
  14. public int Period { get; set; }
  15. public string AssetID { get; set; }
  16. public int Interest { get; set; }
  17. public string PolicyID { get; set; }
  18. public int Principal { get; set; }
  19. public string BorrowerPKH { get; set; }
  20. }
英文:

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

  1. List&lt;Data&gt; data = JArray.Parse(json)
  2. .Select(ja =&gt; new Data {
  3. Principal = (string)ja[&quot;Principal&quot;],
  4. Listing = JObject.Parse((string)ja[&quot;json&quot;]).ToObject&lt;ListingItem&gt;()
  5. }).ToList();

classes

  1. public class Data
  2. {
  3. public ListingItem Listing { get; set; }
  4. public string Principal { get; set; }
  5. }
  6. public class ListingItem
  7. {
  8. public Listing Listing { get; set; }
  9. public int Deadline { get; set; }
  10. public string LenderPKH { get; set; }
  11. }
  12. public class Listing
  13. {
  14. public int Period { get; set; }
  15. public string AssetID { get; set; }
  16. public int Interest { get; set; }
  17. public string PolicyID { get; set; }
  18. public int Principal { get; set; }
  19. public string BorrowerPKH { get; set; }
  20. }
  21. </details>

huangapple
  • 本文由 发表于 2023年2月24日 15:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定