skip to Main Content

I am using NewtonJson for serializing object. I want to serlize a object which has two properties one is normal string and the second property is dictionary of some items.

I am expecting a result something like this:

"Company": {
            "Id": "1393",
            "emp1": {
                "email": "[email protected]",
                "firstName": "test1",
                "lastName": "test1",
                "title": "Mr"
            },
            "emp2": {
                "email": "[email protected]",
                "firstName": "test2",
                "lastName": "test2",
                "title": "Ms"
            }
        }

but I am getting output like below:

"Company": {
            "Id": "1393",
            "employees": {
                "emp1": {
                    "email": "[email protected]",
                    "firstName": "test1",
                    "lastName": "test1",
                    "title": "Mr"
                 },
                 "emp2": {
                    "email": "[email protected]",
                    "firstName": "test2",
                    "lastName": "test2",
                    "title": "Ms"
              }
            }
        }

Here is my Code:

public string GetCompany(Dictionary<string, Employee> employees)
        {
            var company = JsonConvert.SerializeObject(new
            {
                Id = "1393",
                employees
            });

            return company;
        }

3

Answers


  1. By creating the anonymous object, your adding another property named employees.

    Why not just append Id to the dictionary?

    e.g.

    var employeesCopy = employees
        .ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value);
    
    employeesCopy["Id"] = "1393";
    
    var company = JsonConvert.SerializeObject(employeesCopy);
    

    This makes a copy of employees and adds a new key "Id" before serialising.

    Login or Signup to reply.
  2. Create a Poco (Plain Old C# Object). Make sure to add [JsonExtensionData] attribute.

    public class Poco
    {
        public string Id { get; set; } = string.Empty;
    
        [JsonExtensionData]
        public Dictionary<string, object> Employees { get; set; } = new();
    }
    

    And then serialize it

    var employees = new Dictionary<string, object>()
    {
        { Path.GetRandomFileName(), new Employee() },
        { Path.GetRandomFileName(), new Employee() }
    };
    
    var company = JsonConvert.SerializeObject(new Poco { Id = "1393", Employees = employees });
    
    Login or Signup to reply.
  3. Unfortunately it is not clear what is your goal. If you just need a json string , you can try this code

    public string GetCompany(string id, Dictionary<string, Employee> employees)
    {
        var e = JObject.FromObject(employees);
        e.AddFirst(new JProperty( "Id", id));
        return e.ToString();
    }
    

    but probably you need something bigger, so maybe it makes some sense to create a JObject in order to add it to another part of you data. In the end you can serialize it calling ToString() function

    public JObject GetCompany(string id, Dictionary<string, Employee> employees)
    {
        JObject jo = JObject.FromObject(employees);
        jo.AddFirst(new JProperty( "Id", id));
        return jo;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search