反序列化List Json C中的列表#

时间:2017-08-29 17:02:59

标签: c# json list deserialization

我有JSON中的问题列表。每个都包含答案列表。当我反序列化它时,答案列表不会反序列化。我只是用空的答案列表来提问。我该如何解决这个问题?

我的反序列化

List<QuestionTxt> myDeserializedObjList = (List<QuestionTxt>)Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(@"d:\test.txt"), typeof(List<QuestionTxt>));

JSON

[
    {
        "answers": [
            {
                "answer": "1",
                "text": "First"
            },
            {
                "answer": "Second",
                "text": "2"
            }
        ],
        "cost": 1.0,
        "count": 2,
        "image": false,
        "imagePath": null,
        "name": "Numbers",
        "about": "Numbers",
        "solve": false,
        "type": "Сonformity",
        "Id": "cnf0"
    },
    {
        "answers": [
            {
                "answer": "+",
                "text": "One"
            },
            {
                "answer": "-",
                "text": "Two"
            }
        ],
        "cost": 1.0,
        "count": 2,
        "image": false,
        "imagePath": null,
        "name": "Numbers 2",
        "about": "Numbers 2",
        "solve": false,
        "type": "One",
        "Id": "rdb0"
    }
]

QuestionTxt

Answer class

答案

class Answer
{
    private string text_;
    private string answer_;

    public Answer(string text, string answer)
    {
        this.text = text;
        this.answer = answer;
    }

    public string answer { get => answer_; set => answer_ = value; }
    public string text { get => text_; set => text_ = value; }
}

2 个答案:

答案 0 :(得分:3)

你的答案类需要有一个不带这样参数的重载。当Newtonsoft尝试实例化对象时,它不知道要将什么交给构造函数的参数。

public class Answer
{
     public Answer()
     {
     }
     public Answer(string text, string answer)
     {
         this.text = text;
         this.answer = answer;
     }

     public string AnswerName { get;set; }
     public string Text { get;set; }
}

你的QuestionTxt类有这个: private List<Answer> answers_ = new List<Answer)(); 尝试创建该行private List<Answer> answers_;,因为这不在构造函数中。然后你的内部属性与另一个答案中指出的名称相同。 我会复制它并修复它,但它是一个图像,我不打算输入整个事情。

编辑:添加了有关QuestionTxt类的信息。 Fabio在评论中是对的,为了改进你的代码,属性可以有自动访问器。同时将answer属性更改为AnswerName以将其与类名分开以便清楚。

答案 1 :(得分:3)

属性QuestionTxt.answers应为public(您有internal

public class QuestionTxt
{
    public List<Answer> answers { get; set; }  // notice public!
    public string name { get; set; }

    public QuestionTxt()
    {
        answers = new List<Answer>();
    }
}

或者,如果您希望保留internal,请使用JsonPropertyAttribute

public class QuestionTxt
{
    [JsonProperty()]
    internal List<Answer> answers { get; set; }  // notice internal!
    public string name { get; set; }

    public Question()
    {
        answers = new List<Answer>();
    }
}

当然,您想要反序列化的所有类都应该具有无参数构造函数。

作为JsonPropertyAttribute的通知,您可以自由地使用属性名称遵循.Net约定,而不违反json约定

public class QuestionTxt
{
    [JsonProperty("answers")]
    public List<Answer> Answers { get; set; }

    [JsonProperty("name")]
    public string Name{ get; set; }

    public Question()
    {
        Answers = new List<Answer>();
    }
}

public class Answer
{
    [JsonProperty("answer")]
    public string Value { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }
}