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

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

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&lt;List&lt;Root&gt;&gt;(serializedObj);

My json string serializedObj is:

[
  {
    &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;,
    &quot;Principal&quot;: null
  },
  {
    &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;,
    &quot;Principal&quot;: 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(&quot;json&quot;)]
    public string Json { get; set; }

    // TODO: Possibly use a different type; we can&#39;t tell what
    // it&#39;s meant to be
    [JsonProperty(&quot;Principal&quot;)]
    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&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:

var roots = JsonConvert.DeserializeObject&lt;List&lt;Root&gt;&gt;(serializedObj);
var usefulRoots = roots
    .Select(root =&gt; JsonConvert.DeserializeObject&lt;UsefulRoot&gt;(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&lt;Data&gt; data = JArray.Parse(json)
				.Select(ja =&gt; new Data {
				Principal = (string)ja[&quot;Principal&quot;],
				Listing = JObject.Parse((string)ja[&quot;json&quot;]).ToObject&lt;ListingItem&gt;()
				}).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>



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:

确定