[英]List to JSON in asp.net
我從服務器接收到錯誤的JSON,它是實際數組的字符串,因此我在使用JSON.parse。 我查看了asp代碼,發現開發人員正在將列表轉換為JSON。 將列表編碼為JSON格式的最佳方法是什么?
我得到的數據:{d:“ [\\\\” [5970,5971,5972,5973,5976,5974,5975,5977,5978],[343,232] \\\\“]”}
數據應如下所示:{d:[[5970,5971,5972,5973,5976,5974,5975,5977,5978],[343,232]]}
這只是JSON的一小部分,但它是5MB的JSON,因此解析需要很多時間。
如果有人能幫助我找出解決方案,我將非常感謝,因為我是UI專家。 http://jsfiddle.net/aavUA/19
asp代碼實際上是在調用c sharp函數:
public static string GetLevelChildsList(string strInputString)
{
List<IndexReleationship> inflationRelationship = SessionManager.InflationRelation;
string[] inputArguments = strInputString.Split('~');
int intInflationModelLevelID = int.Parse(inputArguments[0].Trim());
List<string> lstResultString = new List<string>();
List<List<int>> strList = new List<List<int>>();
List<int> strInflationHideIdList = new List<int>();
List<int> strInflationShowIdList = new List<int>();
List<int> strActualLevelids = new List<int>();
List<int> selectedLevelids = new List<int>();
for (int count = 0; count < inflationRelationship.Count; count++)
{
if (inflationRelationship[count].IsReleatedIndex > intInflationModelLevelID)
{
if (!strInflationHideIdList.Contains(inflationRelationship[count].ParentIndexID))
{
strInflationHideIdList.Add(inflationRelationship[count].ParentIndexID);
}
if (!strInflationHideIdList.Contains(inflationRelationship[count].ChildIndexID))
{
strInflationHideIdList.Add(inflationRelationship[count].ChildIndexID);
}
}
else if (inflationRelationship[count].IsReleatedIndex == intInflationModelLevelID
&& inflationRelationship[count].IsReleatedIndex != 1169)
{
if (!strActualLevelids.Contains(inflationRelationship[count].ChildIndexID))
{
strActualLevelids.Add(inflationRelationship[count].ChildIndexID);
}
}
else
{
if (!strInflationShowIdList.Contains(inflationRelationship[count].ParentIndexID))
{
strInflationShowIdList.Add(inflationRelationship[count].ParentIndexID);
}
if (!strInflationShowIdList.Contains(inflationRelationship[count].ChildIndexID))
{
strInflationShowIdList.Add(inflationRelationship[count].ChildIndexID);
}
}
}
strList.Add(strInflationHideIdList);
strList.Add(strInflationShowIdList);
strList.Add(strActualLevelids);
selectedLevelids.AddRange(strInflationShowIdList);
selectedLevelids.AddRange(strActualLevelids);
string strResult = GetSessionInflationModels(selectedLevelids);
lstResultString.Add(strList.ToJson());
lstResultString.Add(strResult);
return lstResultString.ToJson();
}
我建議您使用JSON.NET庫將列表序列化為JSON,而不是嘗試自己創建JSON。 如今,這幾乎是.NET中處理JSON的標准方法:
然后,您可以創建一個這樣的對象(為了使本示例保持簡單,我將忽略一些常見的.NET約定,例如大寫字母)
public class MyObject
{
public List<List<int>> d { get; set; }
}
並使用您的值填充,然后使用:
MyObject myObj = new MyObject();
myObj.d = strList;
string output = JsonConvert.SerializeObject(myObj);
該字符串將是您要返回的有效JSON。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.