反序列化JSON,该JSON可能是C#中的整数或字符串列表



我正在使用C#来反序列化JSON字符串的集合。每个字符串都有一个属性employer_normalized,该属性应该包含stringList<int>,其中List中的值总是正的。在某些情况下,employer_normalized被设置为-1,所以我想用一些将employer_normalized设置为null的行为来覆盖这些情况。

这是我的课:

public class EmployerNormalized
{
  public string company;
  public List<int> code;
}

良好的JSON

"employer_normalized": {
        "company": "self",
        "code": [
          "4581 ",
          "6732 ",
          "9121",
          "9999 ",
          "5947 ",
          "8322 ",
          "8351 ",
          "7335 ",
          "9999 ",
          "4225 ",
          "8399 "
        ]
      }

错误的JSON

"employer_normalized": -1

我目前正在使用Json.NET来进行JSON解析。解决这个问题的最佳解决方案是什么?如果是-1,是否最好将employer_normalized值设置为null?如果是,我该怎么做?

您可以使用自定义JsonConverter来处理这种情况。无论您在哪里期望EmployerNormalized,转换器都可以检查该属性的值是否为-1并返回null,否则正常地反序列化它。

这是转换器的代码:

public class EmployerNormalizedConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(EmployerNormalized));
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Object)
        {
            return token.ToObject<EmployerNormalized>();
        }
        return null;
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

这里有一个演示如何使用它:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""good"" : {
                ""company"": ""self"",
                ""code"": [
                    ""4581 "",
                    ""6732 "",
                    ""9121"",
                    ""9999 "",
                    ""5947 "",
                    ""8322 "",
                    ""8351 "",
                    ""7335 "",
                    ""9999 "",
                    ""4225 "",
                    ""8399 ""
                ]
            },
            ""bad"" : -1
        }";
        Wrapper wrapper =
            JsonConvert.DeserializeObject<Wrapper>(json, 
                new EmployerNormalizedConverter());
        DumpEmployer("good", wrapper.good);
        DumpEmployer("bad", wrapper.bad);
    }
    private static void DumpEmployer(string prop, EmployerNormalized emp)
    {
        Console.WriteLine(prop);
        if (emp != null)
        {
            Console.WriteLine("  company: " + emp.company);
            Console.WriteLine("  codes: " + 
                string.Join(", ", emp.code.Select(c => c.ToString())));
        }
        else
            Console.WriteLine("  (null)");
    }
    public class Wrapper
    {
        public EmployerNormalized good { get; set; }
        public EmployerNormalized bad { get; set; }
    }
    public class EmployerNormalized
    {
        public string company;
        public List<int> code;
    }
}

这是输出:

good
  company: self
  codes: 4581, 6732, 9121, 9999, 5947, 8322, 8351, 7335, 9999, 4225, 8399
bad
  (null)

重要提示:您可能会想用[JsonConverter(typeof(EmployerNormalizedConverter))]来装饰EmployerNormalized类,但如果这样做,那么当前形式的转换器最终将递归地调用自己,直到它用StackOverflowException出错。如果需要/想要使用该属性,则需要更改转换器中ReadJson方法的代码,以便它手动创建EmployerNormalized类的实例并单独填充其所有属性,而不是调用token.ToObject<EmployerNormalized>()。以下是ReadJson的另一个版本,可以避免递归问题:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    JToken token = JToken.Load(reader);
    if (token.Type == JTokenType.Object)
    {
        EmployerNormalized employer = new EmployerNormalized();
        employer.company = token["company"].ToString();
        employer.code = token["code"].ToObject<List<int>>();
        return employer;
    }
    return null;
}

我知道这很糟糕,但您可以首先尝试用好的JSON的结构将JSON string解析为Object,如果解析错误,则解析为第二种情况(坏的JSON)。

相关内容

  • 没有找到相关文章

最新更新