您好我正在使用asp.net web api应用程序创建Web服务,我将以json格式返回结果,看起来像
[{"Name":"xxxxx","Age":"10"},{"Name":"yyyy","Age":"20"},{"Name":"zzzzz","Age":"30"}]
但我想输出
{"Name":"xxxxx","Age":"10"}
{"Name":"yyyy","Age":"20"}
{"Name":"zzzzz","Age":"30"}
任何人都可以告诉我如何实现这个目标吗?
修改
这是我的代码
public class Example
{
List<ResultData> resultdata = new List<ResultData>();
public List<ResultData> Records(String value)
{
SqlCommand command = new SqlCommand("Select * from tablename where column=" +value + " LIMIT 1000");
SqlDataReader reader = command.ExecuteReader();
reader.FetchSize = int.MaxValue;
SqlResultSet result = reader.FetchResult();
foreach (SqlRecord row in result)
{
resultdata.Add(new ResultData() { Name = row[0].ToString(),Age = row[1].ToString() });
}
return resultdata;
}
}
我的webapiconfig.cs看起来像这样
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
}
}
此外,我尝试返回一个像json字符串的字符串,但它失败了,我已经转义了双引号,但结果中返回带有转义符号的引号