Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将列表对象序列化为JSON#_C#_Json_Serialization - Fatal编程技术网

C# 使用C将列表对象序列化为JSON#

C# 使用C将列表对象序列化为JSON#,c#,json,serialization,C#,Json,Serialization,我需要一些关于如何使用Dapper将数据导入多个列表对象,然后将列表序列化为正确的JSON格式的建议。请分享你的建议。谢谢 1。JSON格式示例 { "students": [ { "studentId": "", "emailAddresses": [ { "emailAddress": "" } ], "lastName": "", "directory":

我需要一些关于如何使用Dapper将数据导入多个列表对象,然后将列表序列化为正确的JSON格式的建议。请分享你的建议。谢谢

1。JSON格式示例

{
  "students": [
    {
      "studentId": "",
      "emailAddresses": [
        {
          "emailAddress": ""
        }
      ],      
      "lastName": "",
      "directory": {
        "addresses": [
          {
            "city": "",
            "state": ""
          }
        ],      
      }     
    }
  ]
}
 public List<Student> GetStudentData()
 {
     List<Student> dataStudent;
     using (IDbConnection connection = RepositoryHelper.OpenConnection())
     {
         dataStudent = connection.Query<Student, EmailAddresses, Student>("mystoredprocedure",
            (c, cc) =>
            {
                c.EmailAddresses = cc;
                return c;
            }, splitOn: "EmailAddress").ToList();

        }

    return dataStudent;
}
2。课程

public class Student
{
    public string StudentId { get; set; }
    public string Gender { get; set; }  
    public List<EmailAddresses> emailAddresses { get; set; }
}

public class EmailAddresses
{
    public string EmailAddress { get; set; }    
}
4。我正在尝试实现的简洁代码

{
  "students": [
    {
      "studentId": "",
      "emailAddresses": [
        {
          "emailAddress": ""
        }
      ],      
      "lastName": "",
      "directory": {
        "addresses": [
          {
            "city": "",
            "state": ""
          }
        ],      
      }     
    }
  ]
}
 public List<Student> GetStudentData()
 {
     List<Student> dataStudent;
     using (IDbConnection connection = RepositoryHelper.OpenConnection())
     {
         dataStudent = connection.Query<Student, EmailAddresses, Student>("mystoredprocedure",
            (c, cc) =>
            {
                c.EmailAddresses = cc;
                return c;
            }, splitOn: "EmailAddress").ToList();

        }

    return dataStudent;
}
公共列表GetStudentData()
{
列表数据学生;
使用(IDbConnection connection=RepositoryHelper.OpenConnection())
{
dataStudent=connection.Query(“MyStoredProcess”,
(c,cc)=>
{
c、 EmailAddresses=cc;
返回c;
},splitOn:“EmailAddress”).ToList();
}
返回数据学生;
}
4。序列化为JSON

static void Main(string[] args)
{            
    List<Student> students = GetStudentData();
    var json = new System.Web.Script.Serialization.JavaScriptSerializer()
                                                  .Serialize(students);
}
static void Main(字符串[]args)
{            
列出学生=GetStudentData();
var json=new System.Web.Script.Serialization.JavaScriptSerializer()
.连载(学生);
}

第一个问题是调用存储过程时没有指定命令类型是存储过程。这将错误地将进程名转换为sql命令,这显然会失败

然而,根据您最新的评论,第二个问题是您错误地使用了dapper的多映射器功能。此功能用于将多个表映射到多个对象。但是,您的存储过程似乎给了您一个展平的对象,您需要根据学生id分解唯一的记录,将电子邮件地址映射到您的电子邮件地址属性

因为我们可以动态地映射数据,所以Dynamic在这方面非常有效

public List<Student> GetStudentData()
{
    List<Student> dataStudent;
    using (IDbConnection connection = RepositoryHelper.OpenConnection())
    {
        dataStudent = connection.Query<dynamic>(
            "mystoredprocedure", 
            commandType: CommandType.StoredProcedure)
                .GroupBy(x => x.StudentId)
                .Select(x => new Student 
                    { 
                        StudentId = x.First().StudentId, 
                        Gender = x.First().Gender,
                        emailAddresses = x.Select(ea => new EmailAddresses 
                            { 
                                EmailAddress = ea.emailAddresses 
                            }).ToList()
                    }).ToList();

        return dataStudent;
    }
}
公共列表GetStudentData()
{
列表数据学生;
使用(IDbConnection connection=RepositoryHelper.OpenConnection())
{
dataStudent=connection.Query(
“mystoredprocedure”,
commandType:commandType.StoredProcess)
.GroupBy(x=>x.StudentId)
.选择(x=>new Student
{ 
StudentId=x.First().StudentId,
性别=x.第一().性别,
emailAddresses=x。选择(ea=>new emailAddresses
{ 
EmailAddress=ea.emailAddresses
})托利斯先生()
}).ToList();
返回数据学生;
}
}

好的,那么实际问题是什么?您显示的代码是否无效?它在哪一点上失败了?它怎么会失败?简洁的代码失败了。连接查询正在抱怨EmailAddresses对象。dataStudent=connection.QueryComplaining如何?它给了你什么错误信息?它是否在编译时给您一个错误?还是运行时?您可能只想将问题集中在这一部分,然后如果您在以后遇到了问题(例如序列化),请就这一部分单独提问。您的代码非常完美。这正是我所需要的,数据正在毫无问题地转换为JSON。令人惊叹的。非常感谢,希望你有美好的一天,你应得的。你好,大卫L。你觉得你能帮我写这篇文章吗?你在这方面帮了我大忙one@1111lt我为你补充了一个答案。希望它能有所帮助:)。