C# 如何使用C创建复杂的JSON对象#

C# 如何使用C创建复杂的JSON对象#,c#,json,C#,Json,我有这样一个JSON结构: { "name": "flare", "children": [ { "name": "analytics", "children": [ { "name": "cluster", "children": [ {"name": "AgglomerativeCluster", "size": 3938}, {"name": "MergeEdge", "size": 743} ] }

我有这样一个JSON结构:

{
 "name": "flare",
 "children": [
  {
   "name": "analytics",
   "children": [
    {
     "name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "MergeEdge", "size": 743}
     ]
    },
    {
     "name": "graph",
     "children": [
      {"name": "BetweennessCentrality", "size": 3534},
      {"name": "SpanningTree", "size": 3416}
     ]
    },
    {
     "name": "optimization",
     "children": [
      {"name": "AspectRatioBanker", "size": 7074}
     ]
    }
   ]
  },
  {
   "name": "animate",
   "children": [
    {"name": "Easing", "size": 17010},
    {"name": "FunctionSequence", "size": 5842},
    {
     "name": "interpolate",
     "children": [
      {"name": "ArrayInterpolator", "size": 1983},
      {"name": "RectangleInterpolator", "size": 2042}
     ]
    },
    {"name": "ISchedulable", "size": 1041},
    {"name": "Tween", "size": 6006}
   ]
  }
 ]
}

如何使用C#生成这样的JSON,我发现了如何生成JSON数组,但这就是我的全部。我不知道怎样做
孩子“:
属性,或者我不知道如何创建由其他JSON对象或其他JSON数组组成的JSON对象。你能给我一点动力,让我可以生成这样一个JSON对象吗?谢谢

可用于序列化/反序列化复杂对象。

可用于序列化/反序列化复杂对象。

根据需要定义类:

public class Human
{
  public string name { get; set; }
  public int size { get; set; }
  public IEnumerable<Human> children { get; set; }
}
公共类人员
{
公共字符串名称{get;set;}
公共整数大小{get;set;}
公共IEnumerable子项{get;set;}
}

然后使用Newtonsoft、Json.NET或任何其他库将其作为Json获取。

根据需要定义您的类:

public class Human
{
  public string name { get; set; }
  public int size { get; set; }
  public IEnumerable<Human> children { get; set; }
}
公共类人员
{
公共字符串名称{get;set;}
公共整数大小{get;set;}
公共IEnumerable子项{get;set;}
}

然后使用Newtonsoft、Json.NET或任何其他库将其作为Json获取。

如果您使用
Json.NET
(您应该这样做),您可以创建如下类:

public class MyObject
{   
    public string Name { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public List<MyObject> Children { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? Size { get; set; }
}
这将为您提供(为便于阅读,请按事实进行格式化):

几点注意:

NullValueHandling=NullValueHandling.Ignore
用于禁止包含值为null的属性。如果您不关心
Name=“flare”
具有
大小:null的对象,则无需担心它。但如果这样做,使用它可以节省至少三个基本相同但缺少某些属性的不同对象


CamelCasePropertyNamesContractResolver
将自动以驼峰形式显示属性名称。您可以将
MyObject
中的属性设置为驼峰大小写,但这不是.NET标准。如果你愿意打破传统,那你就不需要了。另一种选择是在
JsonPropertyAttribute
中为每个.NET属性设置
PropertyName

如果使用
Json.NET
(而且应该),可以创建如下类:

public class MyObject
{   
    public string Name { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public List<MyObject> Children { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? Size { get; set; }
}
这将为您提供(为便于阅读,请按事实进行格式化):

几点注意:

NullValueHandling=NullValueHandling.Ignore
用于禁止包含值为null的属性。如果您不关心
Name=“flare”
具有
大小:null的对象,则无需担心它。但如果这样做,使用它可以节省至少三个基本相同但缺少某些属性的不同对象


CamelCasePropertyNamesContractResolver
将自动以驼峰形式显示属性名称。您可以将
MyObject
中的属性设置为驼峰大小写,但这不是.NET标准。如果你愿意打破传统,那你就不需要了。另一种选择是在
JsonPropertyAttribute
中为每个.NET属性设置
PropertyName

对于匿名类型,您可以定义对象层次结构,这几乎与普通JSON一样简单。然后使用以下命令序列化它:

对于更复杂的层次结构,您可能需要涉及动态类型。这是您的原始对象:

var obj = new {
    name = "flare",
    children = new[] {
        new {
            name = "analytics",
            children = new dynamic [] {
                new {
                    name = "cluster",
                    children = new dynamic [] {
                        new { name = "AgglomerativeCluster", size = 3938},
                        new { name = "MergeEdge", size = 743},
                    }
                },
                new {
                    name = "graph",
                    children = new dynamic [] {
                        new { name = "BetweennessCentrality", size = 3534},
                        new { name = "SpanningTree", size = 3416},
                    }
                },
                new {
                    name = "optimization",
                    children = new dynamic [] {
                        new { name = "AspectRatioBanker", size = 7074},
                    }
                },
            }
        },
        new {
            name = "animate",
            children = new dynamic [] {
                new { name = "Easing", size = 17010},
                new { name = "FunctionSequence", size = 5842},
                new {
                    name = "interpolate",
                    children = new [] {
                    new { name = "ArrayInterpolator",  size = 1983},
                    new { name = "RectangleInterpolator", size = 2042}
                    }
                },
                new { name = "ISchedulable", size = 1041},
                new { name = "Tween", size = 6006},
            }
        },
    }
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);

演示:

对于匿名类型,您可以定义对象层次结构,这几乎与使用普通JSON一样简单。然后使用以下命令序列化它:

对于更复杂的层次结构,您可能需要涉及动态类型。这是您的原始对象:

var obj = new {
    name = "flare",
    children = new[] {
        new {
            name = "analytics",
            children = new dynamic [] {
                new {
                    name = "cluster",
                    children = new dynamic [] {
                        new { name = "AgglomerativeCluster", size = 3938},
                        new { name = "MergeEdge", size = 743},
                    }
                },
                new {
                    name = "graph",
                    children = new dynamic [] {
                        new { name = "BetweennessCentrality", size = 3534},
                        new { name = "SpanningTree", size = 3416},
                    }
                },
                new {
                    name = "optimization",
                    children = new dynamic [] {
                        new { name = "AspectRatioBanker", size = 7074},
                    }
                },
            }
        },
        new {
            name = "animate",
            children = new dynamic [] {
                new { name = "Easing", size = 17010},
                new { name = "FunctionSequence", size = 5842},
                new {
                    name = "interpolate",
                    children = new [] {
                    new { name = "ArrayInterpolator",  size = 1983},
                    new { name = "RectangleInterpolator", size = 2042}
                    }
                },
                new { name = "ISchedulable", size = 1041},
                new { name = "Tween", size = 6006},
            }
        },
    }
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented);

演示:

您可以使用多种方法,但我发现的一种简单方法是这样的

  • 创建您的主类
  • 创建您的子类
  • 填充主子
  • 获取JSON,如下所示
  • 如果需要获得子对象数组的子对象详细信息的传递列表,您将看到预期的JSON
  • 公共类主控单元头
    {
    公共字符串名称{get;set;}
    公共字符串hdrdetail{get;set;}
    }
    公共类子类详细信息
    {
    公共字符串childname{get;set;}
    公共字符串chdetail{get;set;}
    }
    
    var myjson=new
    {
    主标题=主标题,
    子详细信息=子详细信息
    };
    var jsonContent=JsonConvert.SerializeObject(myjson);
    
    你可以用很多方法,但我发现一个简单的方法是这样的

  • 创建您的主类
  • 创建您的子类
  • 填充主子
  • 获取JSON,如下所示
  • 如果需要获得子对象数组的子对象详细信息的传递列表,您将看到预期的JSON
  • 公共类主控单元头
    {
    公共字符串名称{get;set;}
    公共字符串hdrdetail{get;set;}
    }
    公共类子类详细信息
    {
    公共字符串childname{get;set;}
    公共字符串chdetail{get;set;}
    }
    
    var myjson=new
    {
    主标题=主标题,
    子详细信息=子详细信息
    };
    var jsonContent=JsonConvert.SerializeObject(myjson);
    
    是否要获取现有的c#对象并将其序列化为json?如果您将现有的json字符串加载到此站点,它将创建C#对象所需的类。手动编写json似乎很容易出错。为什么不直接序列化一个类呢?创建一个具有
    名称
    子项
    大小
    属性的类,并对其进行序列化。是否要将现有的c#对象序列化为json?如果您将现有的json字符串加载到此站点,它将创建C#对象所需的类。手动编写json似乎很容易出错。为什么不直接序列化一个类呢?创建一个具有
    名称
    子类
    大小
    属性的类,并对其进行序列化。这会给你一些类似于
    {“Name”:“aggregativecluster”,“Size”:3938,“Children”:null}
    的东西,这可能是操作的问题,也可能不是。这会给你一些类似于
    {“Name”的东西:“凝聚体
    
    var obj = new {
        name = "flare",
        children = new[] {
            new {
                name = "analytics",
                children = new dynamic [] {
                    new {
                        name = "cluster",
                        children = new dynamic [] {
                            new { name = "AgglomerativeCluster", size = 3938},
                            new { name = "MergeEdge", size = 743},
                        }
                    },
                    new {
                        name = "graph",
                        children = new dynamic [] {
                            new { name = "BetweennessCentrality", size = 3534},
                            new { name = "SpanningTree", size = 3416},
                        }
                    },
                    new {
                        name = "optimization",
                        children = new dynamic [] {
                            new { name = "AspectRatioBanker", size = 7074},
                        }
                    },
                }
            },
            new {
                name = "animate",
                children = new dynamic [] {
                    new { name = "Easing", size = 17010},
                    new { name = "FunctionSequence", size = 5842},
                    new {
                        name = "interpolate",
                        children = new [] {
                        new { name = "ArrayInterpolator",  size = 1983},
                        new { name = "RectangleInterpolator", size = 2042}
                        }
                    },
                    new { name = "ISchedulable", size = 1041},
                    new { name = "Tween", size = 6006},
                }
            },
        }
    };
    var json = JsonConvert.SerializeObject(obj, Formatting.Indented);