我有一個類型爲Facility的列表,如下所示。將列表<CustomObject>轉換爲JSON
我想獲取包含帶參數的對象並將該數據序列化爲json的列表。
我目前的實現是我回來,看起來像一個列表的問題:[],[],[],[],[]
....但我的對象有在它的數據,我可以訪問像這樣:_facility[0].FacilityName
或_facility[0].FacilityId
。但是,當我序列化這我沒有得到這些價值觀。
如何向下鑽取以獲取此數據並對其進行序列化?
Facility.cs
被稱爲填充該基金類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;
}
現在我看到在我的提示框的JSON在我的jQuery的完整功能
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;
}
}
代碼。如何將這個數組賦給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>
作爲調試步驟,嘗試使用JavaScriptSerializer。你有預期的結果嗎? –
'設施:列表'?這讓我的大腦受到傷害。 :( –
(但嚴重的是,我沒有看到你如何明智地將這樣的類序列化成JSON;你可能有一個'Facility'類和一個'List Children'屬性) –