gpt4 book ai didi

c# - 准时制。序列化为 json 的最佳方式

转载 作者:太空狗 更新时间:2023-10-29 21:35:18 24 4
gpt4 key购买 nike

我需要为 the jit 创建自定义 json图书馆。我应该使用额外的 C# 逻辑还是以某种方式扩展 JsonSerializer。 Json应该是这样的-->

var json = {
"children": [
{
"children": [
{
"children": [],
"data": {
"playcount": "276",
"$color": "#8E7032",
"image": "http://userserve-ak.last.fm/serve/300x300/11403219.jpg",
"$area": 276
},
"id": "album-Thirteenth Step",
"name": "Thirteenth Step"
}
}]

最佳答案

使用 Json.Net

public void Test()
{
Node root = new Node();
Node child = new Node();
Data data = new Data() { Area = 276, Color = "#8E7032", PlayCount = "276", Image = "http://userserve-ak.last.fm/serve/300x300/11403219.jpg" };
Node grandChild = new Node() { Id = "album-Thirteenth Step", Name = "Thirteenth Step", Data = data };

root.Children.Add(child);
child.Children.Add(grandChild);

var json = JsonConvert.SerializeObject(
root,
new JsonSerializerSettings() {
NullValueHandling= NullValueHandling.Ignore,
Formatting= Newtonsoft.Json.Formatting.Indented
});
}

public class Node
{
[JsonProperty("children")]
public List<Node> Children = new List<Node>();

[JsonProperty("data")]
public Data Data;

[JsonProperty("id")]
public string Id;

[JsonProperty("name")]
public string Name;
}

public class Data
{
[JsonProperty("playcount")]
public string PlayCount;

[JsonProperty("$color")]
public string Color;

[JsonProperty("image")]
public string Image;

[JsonProperty("$area")]
public int Area;
}

关于c# - 准时制。序列化为 json 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11050137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com