简体   繁体   中英

Deserialize List in List Json C#

I have list of questions in JSON. Each of them contains list of answers. When I'm deserializing it, list of answers doesn't deserialize. I'm just getting questions with empty lists of answers. How can i fix this?

My deserializing

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

Your answer class needs to have an overload that takes no parameters like this. When Newtonsoft tries to instantiate the object it doesn't know what to hand to the parameters of the constructor.

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

Your QuestionTxt class has this: private List<Answer> answers_ = new List<Answer)(); try making that line private List<Answer> answers_; because this is not in a constructor. Your then have an internal property with the same name as pointed out in the other answer. I would copy it and fix it but it is an image and I'm not going to type out the whole thing.

EDIT: Added info about the QuestionTxt class. Fabio in the comments was right, to improve on your code the properties can have auto accessors. Also changing the answer property to AnswerName to separate it from the class name for clarity.

Property QuestionTxt.answers should be public (you have internal )

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

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

Or if you prefer to keep it internal - use JsonPropertyAttribute

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

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

And of course all classes you want deserialize should have parameterless constructor.

As a notice with JsonPropertyAttribute you will get a freedom to follow .Net conventions with property names, without violating json conventions

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM