返回由新行分隔的json结果

时间:2015-08-13 11:54:02

标签: asp.net json web-services asp.net-web-api

您好我正在使用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字符串的字符串,但它失败了,我已经转义了双引号,但结果中返回带有转义符号的引号

1 个答案:

答案 0 :(得分:0)

问题是,为什么要这样做?你得到一个对象数组,但是你想在一个响应中返回3个对象(根节点),这是不可能的。

也许these指南有帮助。

相关问题