【问题标题】:JSON string validate with more than one classJSON字符串验证多个类
【发布时间】:2020-01-28 11:58:17
【问题描述】:

在我的业务流程中,我可能有不同格式的 JSON 字符串。

例如:

String jsonSring = readValue();//this JSON String may contain Teacher object or Student Object

目前我正在使用这种简单的方法来验证 JSON 是否相对于 TeacherStudent

try{
    Teacher teacher = om.readValue(jsonSring, Teacher.class);
}catch(Exception e){
    Student student = om.readValue(jsonSring, Student.class);
}

验证 JSON 内容的任何简化方法?

【问题讨论】:

标签: java json validation parsing objectmapper


【解决方案1】:

解决方案 1:添加 Type 字段:

添加一个指定对象类型的字段可能是最简单的选择,但您必须能够更改对象才能做到这一点。

public Enum UserType { Teacher, Student, /* possibly other types */ }

public interface ISchoolMember 
{
    public string Name { get; }
    ..
    public UserType Type { get; }
}

然后,一旦你有了 JSON,你就可以将它解析为 JObject 并读取 Type 字段:

public ISchoolMember Deserialize(string jsonString)
{ 
    var o = JObject.Parse(jsonString);
    return (UserType)o["Type"] switch
    {
        UserType.Teacher => JsonConvert.Deserialize<Teacher>(jsonString),
        UserType.Student => JsonConvert.Deserialize<Student>(jsonString),
        _ => throw new ArgumentException("...")
    };
}

解决方案 2:检查特殊字段。

如果无法添加新字段,则可以检查解析后的 JObject 是否包含仅属于两个对象之一的字段:

public void DeserializeAndDoStuff(string jsonString)
{ 
    var teacherOrStudent = JObject.Parse(jsonString);
    if (teacherOrStudent["StudentId"] != null) // it is a student!
    {
        Student s = teacherOrStudent.ToObject<Student>();
        // ... do stuff with the student object
    } 
    else if (teacherOrStudent["TeacherId"] != null) // it is a teacher!
    {
        Teacher t = teacherOrStudent.ToObject<Teacher>();
        // ... do stuff with the teacher object
    }
    else 
    {
        throw new ArgumentException("The given object is neither a teacher or a student.");
    }
}

这两种方法似乎比原始方法更冗长,但有助于摆脱基于异常的编程(这总是不明智的,因为处理异常在资源方面非常昂贵)。

附言
此实现使用 Newtonsoft.Json 库,但我猜其他库也有类似的机制。

【讨论】:

  • 不可能,因为教师和学生的课程已经是现有课程。我不能碰它。
  • @Araf 我更新了答案,添加了另一个可能的解决方案。
  • 没关系,可能有机会来学生或教师id字段为null。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 2022-01-17
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多