Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在序列化为JSON时将类放入对象中_C#_Json_Serialization - Fatal编程技术网

C# 如何在序列化为JSON时将类放入对象中

C# 如何在序列化为JSON时将类放入对象中,c#,json,serialization,C#,Json,Serialization,我有两门课,分别是: public class ipAddress { public object ip { get; set; } } public class Rule { public string name { get; set; } public ipAddress conditions { get; set; } public string action { get; set; } public Boolean enabled { get;

我有两门课,分别是:

public class ipAddress
{
    public object ip { get; set; }
}

public class Rule
{
    public string name { get; set; }
    public ipAddress conditions { get; set; }
    public string action { get; set; }
    public Boolean enabled { get; set; }
    public string statusCode { get; set; }
}
        Rule new_rule = new Rule();
        ipAddress ip_info = new ipAddress();
        ip_info.ip = new { ipAddress = "34.5.6.7.8" };
        new_rule.name = "test";
        new_rule.conditions = ip_info;
        new_rule.action = "ALLOW";
        new_rule.enabled = true;
        new_rule.statusCode = "FORBIDDEN_403";
        var rule_json = JsonConvert.SerializeObject(new_rule);
我为其创建/分配值的代码是:

public class ipAddress
{
    public object ip { get; set; }
}

public class Rule
{
    public string name { get; set; }
    public ipAddress conditions { get; set; }
    public string action { get; set; }
    public Boolean enabled { get; set; }
    public string statusCode { get; set; }
}
        Rule new_rule = new Rule();
        ipAddress ip_info = new ipAddress();
        ip_info.ip = new { ipAddress = "34.5.6.7.8" };
        new_rule.name = "test";
        new_rule.conditions = ip_info;
        new_rule.action = "ALLOW";
        new_rule.enabled = true;
        new_rule.statusCode = "FORBIDDEN_403";
        var rule_json = JsonConvert.SerializeObject(new_rule);
序列化后,我得到此输出

{“name”:“test”,“conditions”:{“ip”:{“ipAddress”:“34.5.6.7.8”},“action”:“ALLOW”,“enabled”:true,“statusCode”:“probled_403”}

而我的预期输出是:

public class ipAddress
{
    public object ip { get; set; }
}

public class Rule
{
    public string name { get; set; }
    public ipAddress conditions { get; set; }
    public string action { get; set; }
    public Boolean enabled { get; set; }
    public string statusCode { get; set; }
}
        Rule new_rule = new Rule();
        ipAddress ip_info = new ipAddress();
        ip_info.ip = new { ipAddress = "34.5.6.7.8" };
        new_rule.name = "test";
        new_rule.conditions = ip_info;
        new_rule.action = "ALLOW";
        new_rule.enabled = true;
        new_rule.statusCode = "FORBIDDEN_403";
        var rule_json = JsonConvert.SerializeObject(new_rule);
{“name”:“test”,“conditions”:[{“ip”:{“ipAddress”:“34.5.6.7.8”}],“action”:“ALLOW”,“enabled”:true,“statusCode”:“probled_403”}

因此唯一的区别是围绕条件值的额外对象。 我怎样才能做到这一点?尝试了不同的方法,但没有成功。
谢谢

您希望条件是数组 因此,您需要声明公共IP地址[]条件

您还为其指定了一个对象,这就是您得到的输出 如果希望json具有数组输出,则必须将其声明为数组

public ipAddress conditions { get; set; }
上述语句表示
条件
是一个对象,对象由
{}
表示。如果希望它是对象的列表/数组(由
[]
表示),则需要将条件定义为数组/列表项

public List<ipAddress> conditions { get; set; }
公共列表条件{get;set;}
条件的赋值对象应如下所示

new_rule.conditions = new List<ipAddress>() { ip_info };
new_rule.conditions=new List(){ip_info};

这将产生您想要的结果。此外,根据命名约定,您的类名和变量名应以大写字母开头。

要获得预期的输出,您的
ipAddress应为数组

public class Rule
{
    public string name { get; set; }
    public ipAddress[] conditions { get; set; }
    public string action { get; set; }
    public Boolean enabled { get; set; }
    public string statusCode { get; set; }
}

Rule new_rule = new Rule();
ipAddress ip_info = new ipAddress();
ip_info.ip = new { ipAddress = "34.5.6.7.8" };
new_rule.name = "test";
new_rule.conditions = new ipAddress[] { ip_info };
new_rule.action = "ALLOW";
new_rule.enabled = true;
new_rule.statusCode = "FORBIDDEN_403";
var rule_json = JsonConvert.SerializeObject(new_rule);
u将从中获得的结果是

{"name":"test","conditions":[{"ip":{"ipAddress":"34.5.6.7.8"}}],"action":"ALLOW","enabled":true,"statusCode":"FORBIDDEN_403"}

如果我将
public ipAddress条件{get;set;}
更改为
public ipAddress[]条件{get;set;}
,那么我将为此编写什么<代码>新建规则条件=ip\U信息