简体   繁体   中英

Convert json to List<> c#

How to convert Json array to list<> c#

[[{
    "QID": 1,
    "Question": "Question",
    "IsMultipel": 0
},
{
    "QID": 2,
    "Question": "Question",
    "IsMultipel": 1
}],
[{
    "QID": 1,
    "A_ID": 1,
    "Answer": "Answer"
},
{
    "QID": 1,
    "A_ID": 2,
    "Answer": "Answer"
},
{
    "QID": 1,
    "A_ID": 3,
    "Answer": "Answer"
},
{
    "QID": 1,
    "A_ID": 3,
    "Answer": "Answer"
}]]

Error:To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that I am doing like:

List<QuestionAndAnswerNewMarge> _QuestionAndAnswerNewMarge = new List<QuestionAndAnswerNewMarge>();
string str="[[{\"QID\":1,\"Question\":\"Question\",\"IsMultipel\":0},{\"QID\":2,\"Question\":\"Question\",\"IsMultipel\":1}],[{\"QID\":1,\"A_ID\":1,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":2,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":3,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":3,\"Answer\":\"Answer\"}]]";
_QuestionAndAnswerNewMarge = JsonConvert.DeserializeObject<List<QuestionAndAnswerNewMarge>>(str).ToList();

public class QuestionAndAnswerNewMarge
{
    public List<QuestionNew> QuestionNew { get; set; }
    public List<AnswerNew> AnswerNew { get; set; }

}
public class QuestionNew
{

    public string QuestionID { get; set; }
    public string Question { get; set; }
    public string IsMultiple { get; set; }
}
public class AnswerNew
{

    public string QuestionID { get; set; }
    public string AnswerID { get; set; }
    public string Answer { get; set; }
}

The JSON doesn't really line up to the types your trying to use. You have two options here and the one to take depends on if you can change the JSON string at all.

If you can, I suggest just manually building up the objects then serializing them. This outputs the Json in the correct form for use and you can setup whoever is generating it to use that format instead.

As an example, I tweaked your objects a bit, created an instance of it, recreated your data, then serialized it:

public class QuestionAndAnswerNewMarge
{
    public List<Question> Questions { get; set; }
    public List<Answer> Answers { get; set; }
}

public class Question
{
    public int QuestionID { get; set; }
    public string QuestionText { get; set; }
    public bool IsMultiple { get; set; }
}
public class Answer
{
    public int QuestionID { get; set; }
    public int AnswerID { get; set; }
    public string AnswerText { get; set; }
}

JSON:

{
  "Questions": [
    {
      "QuestionID": 1,
      "QuestionText": "Question",
      "IsMultiple": false
    },
    {
      "QuestionID": 2,
      "QuestionText": "Question",
      "IsMultiple": true
    }
  ],
  "Answers": [
    {
      "QuestionID": 1,
      "AnswerID": 1,
      "AnswerText": "Answer"
    },
    {
      "QuestionID": 1,
      "AnswerID": 2,
      "AnswerText": "Answer"
    },
    {
      "QuestionID": 1,
      "AnswerID": 3,
      "AnswerText": "Answer"
    },
    {
      "QuestionID": 2,
      "AnswerID": 1,
      "AnswerText": "Answer"
    }
  ]
}

If you are reading the JSON from an external source, then use a utility like json2csharp to generate classes that will de-serialize using the given json example.

This solution is a little dirty but should work with the JSON string you posted

    public void Convert()
    {
        var questionsAnswers = new QuestionsAnswers()
        {
            Answers = new List<Answ>(),
            Questions = new List<Qst>()
        };


        string str = "[[{\"QID\":1,\"Question\":\"Question\",\"IsMultipel\":0},{\"QID\":2,\"Question\":\"Question\",\"IsMultipel\":1}],[{\"QID\":1,\"A_ID\":1,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":2,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":3,\"Answer\":\"Answer\"},{\"QID\":1,\"A_ID\":3,\"Answer\":\"Answer\"}]]";
        var d = JsonConvert.DeserializeObject<dynamic[][]>(str);
        foreach (var objects in d)
        {
            if (objects.All(a => a["A_ID"] == null))
            {
                questionsAnswers.Questions.AddRange(objects.Select(a => new Qst()
                {
                    IsMultipel = a[nameof(Qst.IsMultipel)],
                    Question = a[nameof(Qst.Question)],
                    QID = a[nameof(Qst.QID)]
                }));
            }
            else
            {
                questionsAnswers.Answers.AddRange(objects.Select(a => new Answ()
                {
                    A_ID = a[nameof(Answ.A_ID)],
                    Answer = a[nameof(Answ.Answer)],
                    QID = a[nameof(Answ.QID)]
                }));
            }
        }
    }

    public class QuestionsAnswers
    {
        public List<Qst> Questions { get; set; }
        public List<Answ> Answers { get; set; }
    }

    public class Qst
    {
        public int QID { get; set; }
        public string Question { get; set; }
        public bool IsMultipel { get; set; }
    }

    public class Answ
    {
        public int QID { get; set; }
        public int A_ID { get; set; }
        public string Answer { get; set; }
    }

You could use this. Note that you'll need to update your property names and types in the classes:

    private void Test()
    {
       QuestionAndAnswerNewMarge q = new QuestionAndAnswerNewMarge();

        JArray ja = JArray.Parse(json);

        var ja1 = JArray.Parse(ja[0].ToString());
        var ja2 = JArray.Parse(ja[1].ToString()); 

        q.QuestionNew = JsonConvert.DeserializeObject<List<QuestionNew>>(ja1.ToString());
        q.AnswerNew = JsonConvert.DeserializeObject<List<AnswerNew>>(ja2.ToString());
    }

    public class QuestionAndAnswerNewMarge
    {
        public List<QuestionNew> QuestionNew { get; set; }
        public List<AnswerNew> AnswerNew { get; set; }
    }
    public class QuestionNew
    {
        public int QID { get; set; }
        public string Question { get; set; }
        public int IsMultiple { get; set; }
    }
    public class AnswerNew
    {
        public string QID { get; set; }
        public string A_ID { get; set; }
        public string Answer { 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