这里有两个相关的ASP.NET Web Service JSON问题。我正在使用N层设计的ASP.NET 2.0应用程序。
问题1:
我试图让我的ASP.NET Web服务生成一个多对象JSON响应,其中每个对象都返回了多个对象。我不确定我是否正确解释,所以这里有一个我正在寻找的例子。
[
{"id":1, "CommentDataElement": "Value",
"OldData":{ "Description": "ABC", "Status": "0" },
"NewData":{ "ShortText": "Text here", "LongText": "More text here" }
},
{"id":2, "CommentDataElement": "Value",
"OldData":{ "Description": "DEF", "Status": "1" },
"NewData":{ "ShortText": "Text here", "LongText": "More text here" }
},
{"id":3, "CommentDataElement": "Value",
"OldData":{ "Description": "GHI", "Status": "2" }
},
{"id":4, "CommentDataElement": "Value",
"NewData":{ "ShortText": "Text here", "LongText": "More text here" }
}
]
id
的1和2同时包含Old
和New
个数据集,而id
3只有Old
个数据集和id
4只有New
数据集。
然后我会使用jQuery来处理这个响应。根据{{1}}和Old
数据集的存在,我会生成正确的页面元素。
New
和OldData
的来源位于两个不同的表格中。这可能吗?
问题2:
此外,是否可以让ASP.NET Web服务返回具有两个不同对象结构的JSON字符串?实施例。
NewData
基本上我想要做的是,向服务器发出一个请求,获取截断的数据集(基于分页,假设只返回完整集的20条记录)并获取完整的记录集数,以便我可以生成我的页码(分页)。在我的数据访问层上,是否可以对记录集使用return语句,为完整计数使用[
{...same as above, just cut out for brevity...}
{"id":4, "CommentDataElement": "Value",
"NewData":{ "ShortText": "Text here", "LongText": "More text here" }
},
{"Count":"100"}
]
参数?只是一个想法。
我很确定我在这里写的JSON不合适。如果您认为合适,请随时更新。任何帮助表示赞赏!谢谢!
答案 0 :(得分:1)
我能够解决这个问题。
基本上您创建了类,然后将该类创建为列表。例如......
public class OldData
{
private string _Description = string.empty;
private string _Status = string.empty;
public string Description { get {return _Description;} set {_Description = value; } }
public string Status { get {return _Status;} set {_Status = value; } }
}
public class NewData
{
private string _ShortText = string.empty;
private string _LongText = string.empty;
public string ShortText { get {return _ShortText;} set {_ShortText = value; } }
public string LongText { get {return _LongText;} set {_LongText = value; } }
}
public class Results
{
private int? _ID = null;
private OldData _od = null;
private NewData _nd = null;
public int? ID { get {return _ID;} set {_ID = value; } }
public OldData od { get {return _od;} set {_od = value; } }
public NewData nd { get {return _nd;} set {_nd = value; } }
}
public class Results_List : List<Results>
{
public Results_List() { }
}
然后,您可以创建该类的实例,并使用数据库中的数据填充该实例。然后,JSON响应采用适当的客户端逻辑格式。希望这有助于其他人。享受!