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

时间:2013-12-12 15:37:56

标签: c# json json.net deserialization

我使用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 "
        ]
      }

Bad JSON

"employer_normalized": -1

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

2 个答案:

答案 0 :(得分:3)

您可以使用自定义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)

重要说明:您可能想要使用EmployerNormalized来装饰[JsonConverter(typeof(EmployerNormalizedConverter))]类,但如果您这样做,则转换器将以其当前形式结束以递归方式调用自身,直到它出现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;
}

答案 1 :(得分:1)

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