如何在.Net Core中序列化没有花括号的对象

l7wslrjt  于 2023-02-06  发布在  .NET
关注(0)|答案(4)|浏览(101)

我正在使用NewtonJson序列化对象。我想序列化一个对象,它有两个属性,一个是普通字符串,第二个属性是一些项的字典。
我期待着这样的结果:

"Company": {
            "Id": "1393",
            "emp1": {
                "email": "test1@example.com",
                "firstName": "test1",
                "lastName": "test1",
                "title": "Mr"
            },
            "emp2": {
                "email": "test2@example.com",
                "firstName": "test2",
                "lastName": "test2",
                "title": "Ms"
            }
        }

但我得到的输出如下:

"Company": {
            "Id": "1393",
            "employees": {
                "emp1": {
                    "email": "test1@example.com",
                    "firstName": "test1",
                    "lastName": "test1",
                    "title": "Mr"
                 },
                 "emp2": {
                    "email": "test2@example.com",
                    "firstName": "test2",
                    "lastName": "test2",
                    "title": "Ms"
              }
            }
        }

下面是我的代码:

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

            return company;
        }
db2dz4w8

db2dz4w81#

通过创建匿名对象,您可以添加另一个名为employees的属性。
为什么不直接把Id添加到字典中呢?
例如:

var employeesCopy = employees
    .ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value);

employeesCopy["Id"] = "1393";

var company = JsonConvert.SerializeObject(employeesCopy);

这将生成employees的副本,并在序列化之前添加新密钥"Id"

insrf1ej

insrf1ej2#

下面是一个完整的工作演示,你可以跟随:

public string GetCompany(Dictionary<string, Employee> employees)
{
    var model = new Dictionary<string, object>();
    model["Id"] = "1393";
    foreach (var employee in employees)
    {
        model[employee.Key] = employee.Value;
    }
        
    var company = JsonConvert.SerializeObject(new { Company =model});

    return company;
}
r3i60tvu

r3i60tvu3#

创建一个Poco(Plain Old C#对象)。确保添加[JsonExtensionData]属性。

public class Poco
{
    public string Id { get; set; } = string.Empty;

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

然后将其序列化

var employees = new Dictionary<string, object>()
{
    { Path.GetRandomFileName(), new Employee() },
    { Path.GetRandomFileName(), new Employee() }
};

var company = JsonConvert.SerializeObject(new Poco { Id = "1393", Employees = employees });
nue99wik

nue99wik4#

不幸的是,我们还不清楚你的目标是什么,如果你只是需要一个json字符串,你可以试试下面的代码

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

但是你可能需要更大的东西,所以创建一个JObject以便将它添加到数据的另一部分可能是有意义的,最后你可以通过调用ToString()函数来序列化它

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

相关问题