反序列化后,JSON.NET的某些字段为空

时间:2015-11-28 22:03:07

标签: c# json serialization json.net

大家好,我有一些问题需要了解为什么会发生这种情况

我有这个课程,我正在尝试反序列化:

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;
    }
}

Debug.Log打印:

正确的枚举对象

使用正确的对象更正列表

所有对象都在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);

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

非常感谢

1 个答案:

答案 0 :(得分:2)

认为问题出在参数的名称中:Json.NET要求构造函数的名称与属性的名称相匹配。 在您的示例中,至少您发布的部分,您有一个&#34; id&#34; TextConceptValidationJob上的参数,但参数(和属性)在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; }
}