hs_
hs_

Reputation: 86

How to put an class inside an object while Serializing to JSON

I have two classes made as:

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; }
}

My code to create/assign values to this is:

        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);

after serializing I get this output

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

While my expected output is:

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

so the only difference is the extra object wrapped around the conditions' values. How can I accomplish this? Tried different things but didn't get there. Thanks

Upvotes: 2

Views: 75

Answers (3)

Jawad
Jawad

Reputation: 11364

public ipAddress conditions { get; set; }

The above statement means conditions is an object and objects are represented by { }. If you are expecting it to be a list/array of objects (represented by [ ]), then you will need to define your conditions as an array/list item

public List<ipAddress> conditions { get; set; }

Your assignment object for conditions should look like this,

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

This will produce the result you want. Also, According to the naming conventions, your class name and variable names should start with UpperCase letters.

Upvotes: 3

Loong
Loong

Reputation: 258

For you to get your expected output, your ipAddress should be an array.

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);

The result that u will obtain from this is

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

Upvotes: 0

Tnimni
Tnimni

Reputation: 53

You want the conditions to be an array So you need to declare public IP addresses[] conditions

You also assigned an object to it and that's the output you get If you want the json to have an array output you have to declare it as an array

Upvotes: 0

Related Questions