我有一个 Facility 类型的列表,下面的类。
我想获取包含带有参数的对象的列表并将该数据序列化为 json。
我当前实现的问题是我得到了一个看起来像这样的列表:[],[],[],[],[]
....但是我的对象中有数据,我可以像这样访问:_facility[0].FacilityName
OR _facility[0].FacilityId
。但是当我序列化这个时,我没有得到这些值。
如何深入获取这些数据并对其进行序列化?
设施.cs
public class Facility : List<Facility>
{
[Required]
[Display(Name = "Facility")]
public string FacilityName { get; set; }
public string FacilityIdSource { get; set; }
public Facility()
{
// Default Constructor
}
public Facility(string facilityName, string facilityIdSource)
{
this.FacilityName = facilityName;
this.FacilityIdSource = facilityIdSource;
}
}
为填充 Facility 类而调用的代码
public async Task<string> FacilitiesAsync()
{
ViewBag.SyncOrAsync = "Asynchronous";
var service = new FacilitiesService();
this._facilities = new List<Facility>();
_facilities = await service.GetFacilitiesBySourceDbAsync("param");
string json = "";
for (int i = 0; i < _facilities.Count; i++)
{
json += JsonConvert.SerializeObject(_facilities[i].FacilityName);
}
return json;
}
现在我从我的 jQuery 的完整函数中看到我的警报框中的 json。如何为availableTags分配这个数组?
<script type="text/javascript">
$(function() {
var result = null;
$.ajax({
beforeSend: function () {
alert("Testing");
},
url: "FacilitiesAsync",
success: function (data) {
result = data;
},
complete: function () {
alert(result);
}
});
});
$(function () {
var availableTags = [
// Place results from JSON here
];
$("#tags").autocomplete({
source: availableTags
});
</script>