我关注了this创建 Restful Web 服务,将 JSON 显示为 MySQL 数据库的输出。
我成功完成了,但是数据库中有近 100 个不同名称的表
为了获取数据,我正在使用这个:
ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getAllCustomers")]
List<wsCustomer> GetAllCustomers();
还有这个:
public class Service1 : IService1
{
public List<wsCustomer> GetAllCustomers()
{
NorthwindDataContext dc = new NorthwindDataContext();
List<wsCustomer> results = new List<wsCustomer>();
foreach (Customer cust in dc.Customers)
{
results.Add(new wsCustomer() {
CustomerID = cust.CustomerID,
CompanyName = cust.CompanyName,
City = cust.City
});
}
return results;
}
我正在获取这样的数据输出:
{
"CustomerResult": [
{
"CustomerID ": "12124",
"CompanyName ": "http://www.google.com",
"City ": "xyz"
}
]
}
但这里的问题是对于每个结果,我得到的表名称结果就像我有另一个表
{
"UserResult": [
{
"UserID ": "12124",
"CompanyName ": "http://www.google.com",
"City ": "xyz"
}
]
}
所以这里插入了CustomerResult或UserResult,我希望它作为Result
有没有办法将其显示为所有表的结果。
请推荐我..
最佳答案
您可以使用类似 map 的结构来实现此目的。我不确定 .Net,但在 java 中我们确实是这样的:
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("result","customerList");
resultMap.put("status","success");
您还可以使用 map 。根据我的理解,在您的场景中,它返回表名称作为键,因为您的方法直接返回列表。为了使其成为 json,它需要一个 key 。
关于c# - 如何设置 JSON 对象从结果开始?.Net MySQL JSON Restful Webservice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23867300/