C# 在C中将数据转换为树结构json#
我有一个如下所示的数据表 ,其中我有四列Id、姓名、薪水和RefId 在Refid中,我们存储父对象的Id 数据表的模态类别如下所示C# 在C中将数据转换为树结构json#,c#,json,C#,Json,我有一个如下所示的数据表 ,其中我有四列Id、姓名、薪水和RefId 在Refid中,我们存储父对象的Id 数据表的模态类别如下所示 public class SalaryDetail { public int Id { get; set; } public string Name { get; set; } public System.Nullable<int> Salary { get; set; } public System.Nullable&
public class SalaryDetail
{
public int Id { get; set; }
public string Name { get; set; }
public System.Nullable<int> Salary { get; set; }
public System.Nullable<int> Refid { get; set; }
}
我还制作了三个类来制作树结构,如下所示
public class Child
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class EducationDetail
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class RootObject
{
public bool result { get; set; }
public List<EducationDetail> education_details { get; set; }
}
公共类子类
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
public int refereceId{get;set;}
公共列表子项{get;set;}
}
公共课堂教学细节
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
public int refereceId{get;set;}
公共列表子项{get;set;}
}
公共类根对象
{
公共布尔结果{get;set;}
公共列表教育_详细信息{get;set;}
}
您需要以下类:
public class Child
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class EducationDetail
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class RootObject
{
public bool result { get; set; }
public List<EducationDetail> education_details { get; set; }
}
你需要像这样的课程:
public class Child
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class EducationDetail
{
public int ID { get; set; }
public string name { get; set; }
public int salary { get; set; }
public int refereceId { get; set; }
public List<Child> childs { get; set; }
}
public class RootObject
{
public bool result { get; set; }
public List<EducationDetail> education_details { get; set; }
}
您可以在System.Web.Extensions中使用JavaScriptSerializer。然后使用serialize方法将对象转换为json。System.Web.Extensions的serialize方法将对象转换为json,但树结构呢???@user3235118树结构与json完全无关-首先在C中构造“树”,然后尝试序列化树的对象。你不能直接从原作开始class@MKMohantyJavaScriptSerializer是一个古老的、过时的程序,在涉及日期时不会生成正确的JSon。每个人(包括MS)都使用JSon。NET@Caramiriel我使用了System.Web.Extensions.com的序列化方法,该方法将对象转换为json对象。但不是树结构格式。我希望它成为子-父关系。您可以在System.Web.Extensions中使用JavaScriptSerializer。然后使用serialize方法将对象转换为json。System.Web.Extensions的serialize方法将对象转换为json,但树结构呢???@user3235118树结构与json完全无关-首先在C中构造“树”,然后尝试序列化树的对象。你不能直接从原作开始class@MKMohantyJavaScriptSerializer是一个古老的、过时的程序,在涉及日期时不会生成正确的JSon。每个人(包括MS)都使用JSon。NET@Caramiriel我使用了System.Web.Extensions.com的序列化方法,该方法将对象转换为json对象。但不是树结构格式。我希望它成为子-父关系。为什么您需要
Child2
?很公平,可能有助于让OP知道您使用的工具。您知道可以在VisualStudio中将json粘贴为类吗?我想知道这是否也会犯同样的错误。@BWA是否有任何示例?以便我能与之相关。尝试过它,VS“编辑->粘贴特殊->粘贴JSON作为类”更糟糕。根据问题的输入,创建4个Child
类类型。不过清理起来很容易。@user3235118是如何使用JSON.NET的示例。为什么需要Child2
?很公平,可能有助于让OP知道您使用了哪个工具。您知道可以在VisualStudio中将json粘贴为类吗?我想知道这是否也会犯同样的错误。@BWA是否有任何示例?以便我能与之相关。尝试过它,VS“编辑->粘贴特殊->粘贴JSON作为类”更糟糕。根据问题的输入,创建4个Child
类类型。不过清理起来很容易。@user3235118是如何使用JSON.NET的示例。
static void Main()
{
RootObject ro = new RootObject()
{
result = true,
education_details = new List<EducationDetail>()
};
EducationDetail ed = new EducationDetail()
{
ID = 1,
name = "1",
refereceId = 2,
salary = 3,
childs = null
};
ro.education_details.Add(ed);
ed = new EducationDetail()
{
ID = 2,
name = "2",
refereceId = 2,
salary = 3,
childs = new List<Child>()
};
ro.education_details.Add(ed);
Child c = new Child()
{
ID = 3,
name = "3",
refereceId = 2,
salary = 3,
childs = null
};
ed.childs.Add(c);
string json = JsonConvert.SerializeObject(ro);
}
{
"result": true,
"education_details": [{
"ID": 1,
"name": "1",
"salary": 3,
"refereceId": 2,
"childs": null
},
{
"ID": 2,
"name": "2",
"salary": 3,
"refereceId": 2,
"childs": [{
"ID": 3,
"name": "3",
"salary": 3,
"refereceId": 2,
"childs": null
}]
}]
}