[英]How to serialize a list to JSON
I need to serialize the query from my database into a Json format which looks like this: 我需要将数据库中的查询序列化为Json格式,如下所示:
{
Facilities:[
{
FacilityID:".....",
desc:"....."
},
....
]
}
However, i cant seemed to get the Facilities json array in my codes. 但是,我似乎无法在我的代码中获得Facilities json数组。 How do i edit my codes to show the Facilities? 如何编辑我的代码以显示设施? Please Help, Thank you. 请帮助,谢谢。 My output now: 我现在的输出:
[
{
FacilityID:".....",
desc:"....."
},
....
]
Not sure how does it works if i added this code: 如果我添加以下代码,则不确定如何工作:
Public class FacList
{
public List<FacObject> Facilities;
}
Creating of getters and setters 创建吸气剂和二传手
public class FacObject
{
public string facilityID { get; set; }
public string departmentID { get; set; }
public string description { get; set; }
public string block { get; set; }
public string level { get; set; }
public string name { get; set; }
public string openHours { get; set; }
public string closeHours { get; set; }
public string maxBkTime { get; set; }
public string maxBkUnits { get; set; }
public string minBkTime { get; set; }
public string minBkUnits { get; set; }
public FacObject(string facid, string depid, string desc, string b, string l, string n
,string o ,string c, string maxt, string maxu, string mint, string minu)
{
this.facilityID = facid;
this.departmentID = depid;
this.description = desc;
this.block = b;
this.level = l;
this.name = n;
this.openHours = o;
this.closeHours = c;
this.maxBkTime = maxt;
this.maxBkUnits = maxu;
this.minBkTime = mint;
this.minBkUnits = minu;
}
}
Below is my code which queries from the database and serializes the list of facilities into a json string and passes back to the caller! 下面是我的代码,该代码从数据库查询并将设施列表序列化为json字符串,然后传递回调用方! Please help and advice! 请帮忙和指教! Help would be appreaciated:) 帮助将不胜感激:)
protected void Page_Load(object sender, EventArgs e)
{
//To get the string to search in facility table
string departmentID = Request.QueryString["DepartmentID"];
string block = Request.QueryString["Block"];
string level = Request.QueryString["Level"];
string name = Request.QueryString["Name"];
//creates a list of fac object
List<FacObject> sqlFacList = new List<FacObject>();
//test if correct
//Only select from a certain department
//select the database for the list of facility that contains
//block, level, name (%)
using (var db = new KioskContext())
{
var facilitys = from f in db.Facilitys
where f.Department.DepartmentID == departmentID
&& (f.Block.Contains(block) && f.Level.Contains(level)
&& f.Name.Contains(name))
orderby f.FacilityID
select new { f.FacilityID, f.DepartmentID, f.Description, f.Block,
f.Level, f.Name, f.OpenHours, f.CloseHours, f.MaxBkTime, f.MaxBkUnits, f.MinBkTime, f.MinBkUnits};
foreach (var fac in facilitys)
{
FacObject facobject = new FacObject(fac.FacilityID, fac.DepartmentID, fac.Description, fac.Block, fac.Level,
fac.Name, fac.OpenHours, fac.CloseHours, fac.MaxBkTime, fac.MaxBkUnits, fac.MinBkTime, fac.MinBkUnits);
sqlFacList.Add(facobject);
}
}
//Serialize into json format output (string)
string json = JsonConvert.SerializeObject(sqlFacList, Formatting.Indented);
//codes to pass back the json string to the iPad
Response.Write(json);
}
Actually you are on the right track. 实际上,您在正确的轨道上。 You mentioned in your post you created a class FacList but in your code you are not using it. 您在帖子中提到创建了FacList类,但是在代码中没有使用它。 The example below uses that class produces the exact JSON you're looking for: 下面的示例使用该类生成您要查找的确切JSON:
Sample code: 样例代码:
var sqlFacList = new FacList();
sqlFacList.Facilities = new List<FacObject>();
sqlFacList.Facilities.Add( new FacObject() { facilityID = "12" });
sqlFacList.Facilities.Add(new FacObject() { facilityID = "34" });
string json = JsonConvert.SerializeObject(sqlFacList, Formatting.Indented);
Output: 输出:
{
"Facilities": [
{
"facilityID": "12",
"departmentID": null,
"description": null,
"block": null,
"level": null,
"name": null,
"openHours": null,
"closeHours": null,
"maxBkTime": null,
"maxBkUnits": null,
"minBkTime": null,
"minBkUnits": null
},
{
"facilityID": "34",
"departmentID": null,
"description": null,
"block": null,
"level": null,
"name": null,
"openHours": null,
"closeHours": null,
"maxBkTime": null,
"maxBkUnits": null,
"minBkTime": null,
"minBkUnits": null
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.