【问题标题】:Convert json to List<> c#将 json 转换为 List<> c#
【发布时间】:2016-09-15 15:15:22
【问题描述】:

如何将Json数组转换为列表 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"
}]]

错误:要修复此错误,请将 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; }
}

【问题讨论】:

  • Deserialize JSON with C#的可能重复
  • 您省略了异常的相关部分。如果您对此进行研究,您会发现很多解决方案。

标签: c# json json.net


【解决方案1】:

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,则使用json2csharp 之类的实用程序生成将使用给定 json 示例进行反序列化的类。

【讨论】:

  • 我已经使用替换方法进行了更改,但钢铁得到了同样的错误:{“问题”:[{“QID”:“1”,“问题”:“问题”,“IsMultipel”:“0” },{"QID":"2","Question":"Question","IsMultipel":"1"}], "Answers": [{"QID":"1","A_ID":"1" ,"答案":"答案"},{"QID":"1","A_ID":"2","答案":"答案"},{"QID":"1","A_ID":" 3","Answer":"Answer"},{"QID":"1","A_ID":"3","Answer":"Answer"}]}
【解决方案2】:

这个解决方案有点脏,但应该适用于您发布的 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; }
    }

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 2014-09-09
      • 2019-05-08
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多