将json转换为List<>c#

本文关键字:List json 转换 | 更新日期: 2023-09-27 18:07:12

如何将Json数组转换为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:要修复此错误,要么将JSON更改为JSON对象(例如{"name":"value"}),要么将反序列化类型更改为数组或类型我正在做的是:

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

将json转换为List<>c#

JSON并不符合您尝试使用的类型。这里有两个选项,其中一个取决于您是否可以更改JSON字符串。

如果可以,我建议手动构建对象,然后序列化它们。这将以正确的格式输出Json供使用,您可以设置生成它的人使用该格式。

作为一个例子,我稍微调整了一下你的对象,创建了它的一个实例,重新创建了你的数据,然后序列化它:

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"
    }
  ]
}

如果您是从外部源读取JSON,那么使用json2sharp之类的实用程序来生成类,这些类将使用给定的JSON示例进行反序列化。

这个解决方案有点脏,但应该可以处理您发布的JSON字符串

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

您可以使用这个。注意,您需要更新类中的属性名称和类型:

    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; }
    }
相关文章: