JSON.NET某些字段在反序列化后为null



大家好,我遇到了一些问题,无法理解为什么会发生这种情况

我有一个正在尝试反序列化的类:

public class TextConceptValidationJob : ConceptValidationJob<ConceptTextItem, BooleanTermAnswer> 
{
    [JsonConstructor]
    public TextConceptValidationJob(string id, TaskType taskType, ConceptTextItem wordItem, IList<BooleanTermAnswer> answerContainers) : this (id, taskType, wordItem, answerContainers, new List<string>())
    {
        Debug.Log (id);
        Debug.Log (taskType);
        Debug.Log (wordItem);
        Debug.Log (answerContainers[0].Term);
    }
    public TextConceptValidationJob(string id, TaskType taskType, ConceptTextItem  wordItem, IList<BooleanTermAnswer> answerContainers, IList<string> goldAnswers) : base (id, taskType, wordItem, answerContainers, goldAnswers)
    {
    }
    public string Text
    {
        get
        {
            return item.Text;
        }
    }
    public string Target
    {
        get
        {
            return item.Target;
        }
    }
    protected override string GetAnnotationString<U> (AnswerContainer<U> answerContainer)
    {
        return answerContainer.Query;
    }
    public override string ToString ()
    {
        return base.JobId + "t" + Target;
    }
}

调试日志打印:

正确的枚举对象

具有正确对象的正确列表

所有对象都在TextConceptValidationJob的抽象超类中,如下所示:

[DataContract]
public abstract class Job <I, T, K> where I : Item where T : AnswerContainer<K>
{
    private string jobId;
    private readonly TaskType taskType;
    protected IList<T> answerContainers;
    protected I item;
    public Job (string jobId, TaskType taskType, I item) : this (jobId, taskType, item, new List<string>())
    {
    }
    public Job (string jobId, TaskType taskType, I item, IList<string> goldAnswers)  
    {
        this.jobId = jobId;
        this.taskType = taskType;
        this.task = GetTaskFromType (taskType);
        this.item = item;
        this.goldAnswers = goldAnswers;
    }
    [DataMember(Name = "jobId")]
    public string JobId
    {
        get
        {
            return jobId;
        }
    }
    [DataMember(Name = "item")]
    public I Item
    {
        get
        {
            return item;
        }
    }
    [DataMember(Name = "answerContainers")]
    public IList<T> AnswerContainers
    {
        get
        {
            return answerContainers;
        }
    }
    [DataMember(Name = "task")]
    public TaskType TaskType
    {
        get
        {
            return taskType;
        }
    }
}

这是我无法反序列化的json示例:

{  
   "jobId":"DISAMBIGUATION#2460",
   "item":{  
      "target":"waste",
      "text":"A piece of land where waste materials are dumped",
      "id":"DISAMBIGUATION#2460#44"
   },
   "answerContainers":[  
      {  
         "term":"agriculture (Farming)",
         "id":"DISAMBIGUATION#2460#44",
         "isSent":false,
         "query":"agriculture (Farming)",
         "lang":"EN"
      },
      {  
         "term":"agriculture (Farming)",
         "id":"DISAMBIGUATION#2460#44",
         "isSent":false,
         "query":"agriculture (Farming)",
         "lang":"EN"
      },
      {  
         "term":"agriculture (Farming)",
         "id":"DISAMBIGUATION#2460#44",
         "isSent":false,
         "query":"agriculture (Farming)",
         "lang":"EN"
      },
      {  
         "term":"agriculture (Farming)",
         "id":"DISAMBIGUATION#2460#44",
         "isSent":false,
         "query":"agriculture (Farming)",
         "lang":"EN"
      },
      {  
         "term":"agriculture (Farming)",
         "id":"DISAMBIGUATION#2460#44",
         "isSent":false,
         "query":"agriculture (Farming)",
         "lang":"EN"
      }
   ],
   "task":"DISAMBIGUATION"

对于序列化和反序列化,我使用的是JSON.NET:

    TextConceptValidationJob o = new TextConceptValidationJob("DISAMBIGUATION#2460", TaskType.DISAMBIGUATION, item, list);
    string serialized = JsonConvert.SerializeObject (o);
    JsonConvert.DeserializeObject<TextConceptValidationJob> (serialized);

你知道可能是什么吗?也许我错过了一些愚蠢的东西,但我似乎找不到的问题

非常感谢

I认为问题出在参数的名称上:Json.NET要求构造函数参数的名称与属性的名称匹配。在您的示例中,至少在您发布的部分中,TextConceptValidationJob上有一个"id"参数,但该参数(和属性)在Job超类上被称为jobId。

解决方案:重命名Json.NET调用的构造函数中的参数,以匹配Job超类定义的属性的实际名称。

祝你好运!

编辑:提供的解决方案要求您更改Json的签名。如果不能,您可以在派生类中实现两个属性,id和wordItem,它们应该只复制基本jobId和item属性上的值。

public string id
{
    get { return jobId; }
    set { jobId = value; }
}
public string ConceptTextItem
{
    get { return item; }
    set { item = value; }
}

相关内容

  • 没有找到相关文章

最新更新