【问题标题】:how to write Custom JSon serializer in C#如何在 C# 中编写自定义 JSON 序列化程序
【发布时间】:2015-01-16 07:18:53
【问题描述】:

我正在使用 Newtonsoft json 序列化程序将 json 字符串转换为对象。我的 json 结构如下 -

{
    "EmployeeRecords": {
        "12": {
            "title": "Mr",
            "Name": "John"
        },
        "35":{
            "title": "Mr",
            "Name": "Json"
        }
    }
}

我希望这个 Json 被序列化到下面的类中 -

public class Employee
{
    public string EmployeeNumber { get; set; }
    public string title { get; set; }
    public string Name { get; set; }
}

public class EmployeeRecords
{
    public List<Employee> Employees { get; set; }
}

这里的 Employeenumber 是 12 和 35。

请指导我如何编写自定义序列化程序,它将从父节点读取员工编号并将其包含在子节点的 EmployeeNumber 属性中。

【问题讨论】:

  • 尝试使用这个解决方案:stackoverflow.com/questions/7895105/…
  • 业务规则是否要求您创建该 JSON?它是有效的,但它非常不标准。
  • 可以修改 JSON 吗?不要重新发明轮子!在匹配的类中序列化 JSON,然后将该类转换为您想要的类。
  • 无法使用某些 CQ5 工具修改业务提供的 JSON

标签: c# json serialization json.net jsonserializer


【解决方案1】:

您可以反序列化为字典,然后在循环中分配 EmployeeNumbers。

public class DataModel
{
    public Dictionary<string, Employee> EmployeeRecords { get; set; }
}

反序列化后分配数字:

var records = JsonConvert.DeserializeObject<DataModel>(json);

foreach (var item in records.EmployeeRecords)
{
    item.Value.EmployeeNumber = item.Key;
}

【讨论】:

  • 如何首先反序列化为字典来填充 Employee 对象?
  • @CHash_Mike 更改 EmployeeRecords 类中的属性名称。我已经更新了答案。
  • 嗨 Ufuk,我尝试了这个解决方案,但它无法将 json 序列化为字典。我错过了什么吗?我想知道序列化程序如何知道必须将 json 对象序列化为字典?
  • @CHash_Mike 对象只是 JavaScript 中的字典。我更改了类名,因为您不能拥有同名的属性名称。你能再试一次吗?
  • 感谢@Ufuk.. 解决方案是正确的,但看起来我从 Json 生成的类结构存在问题,集合为空。
【解决方案2】:

您可以使用 LINQ to JSON(JObject 和朋友)轻松完成此操作。这是一个简短但完整的示例:

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

public class Employee
{
    public string EmployeeNumber { get; set; }
    public string title { get; set; }
    public string Name { get; set; }

    public JProperty ToJProperty()
    {
        return new JProperty(EmployeeNumber,
            new JObject {
                { "title", title },
                { "Name", Name }
            });
    }
}

public class EmployeeRecords
{
    public List<Employee> Employees { get; set; }

    public JObject ToJObject()
    {
        var obj = new JObject();
        foreach (var employee in Employees)
        {
            obj.Add(employee.ToJProperty());
        }
        return new JObject {
            new JProperty("EmployeeRecords", obj)
        };
    }
}

class Test
{
    static void Main()
    {
        var records = new EmployeeRecords {
            Employees = new List<Employee> {
                new Employee {
                    EmployeeNumber = "12",
                    title = "Mr",
                    Name = "John"
                },
                new Employee {
                    EmployeeNumber = "35",
                    title = "Mr",
                    Name = "Json"
                },
            }
        };
        Console.WriteLine(records.ToJObject());
    }    
}

它可能不是最简单的代码(如果你愿意改变你的结构,Ufuk 的方法非常棒),但它显示了一切是多么可定制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多