在 Web API 控制器中构建 JSON 响应
Build JSON response in Web API controller
在 WebAPI 项目中,我有一个控制器根据用户输入的值检查产品的状态。
假设他们输入“123”,响应应该是 "status":1,以及产品列表。如果他们输入“321”,则 "status" 为 0,并且是产品列表。
我的问题是,如何在 WebAPI 控制器中正确构建这样的字符串。
[Route("{value:int}")]
public string GetProducts(int value)
{
var json = "";
var products = db.Products;
if (products.Any())
{
foreach (var s in products)
{
ProductApi product = new ProductApi();
product.Name = s.Name;
json += JsonConvert.SerializeObject(supplier);
}
}
var status = db.Status;
if (status.Any())
{
json += "{status:1}";
}
else
{
json += "{status:0}";
}
return json;
}
public class ProductApi
{
public string Name { get; set; }
}
另外,这个output/response有效吗?
[
{
"id":1,
"name":"product name"
},
{
"id":2,
"name":"product name 2"
},
{
"id":3,
"name":"product name 3"
}
]
{
"status": 0
}
正如 raderick 提到的,您不需要创建自己的自定义 JSON 基础架构。
public class ProductApi
{
public int Id {get;set;}
public string Name { get; set; }
}
public class ResponseDTO
{
public int Status {get;set;}
public List<ProductApi> { get; set; }
}
并且在您的 API 操作中,return 像这样:
[Route("{value:int}")]
public ResponseDTO GetProducts(int value)
{
ResponseDTO result = ...// construct response here
return result;
}
下面是 post 的变化:
首先,当你传递一个text/html
请求时,你应该默认你的apireturnJson(这是你要找的吗?),添加这个行到您的 WebApiConfig
class:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
其次,我将代码更改为return一个真实的对象,冒充您的回复:
public class ProductApiCollection
{
public ProductApi[] Products { get; set; }
public byte Status { get; set; }
}
public class ProductApi
{
public string Name { get; set; }
}
方法体:
public ProductApiCollection Get()
{
var result = new ProductApiCollection();
var dbProducts = db.Products;
var apiModels = dbProducts.Select(x => new ProductApi { Name = x.Name } ).ToArray();
result.Products = apiModels;
var status = db.Status.Any() ? 1 : 0;
result.Status = status;
return result;
}
这将导致以下示例 json:
{
"Products": [
{
"Name": "Pork"
},
{
"Name": "Beef"
},
{
"Name": "Chicken"
},
{
"Name": "Salad"
}
],
"Status": 1
}
我强烈建议您不要对此类内容进行手动格式化,而要依赖内置库和第 3 方库。否则,您将重新发明已经可用、经过测试并准备好工作的东西。
在 WebAPI 项目中,我有一个控制器根据用户输入的值检查产品的状态。
假设他们输入“123”,响应应该是 "status":1,以及产品列表。如果他们输入“321”,则 "status" 为 0,并且是产品列表。
我的问题是,如何在 WebAPI 控制器中正确构建这样的字符串。
[Route("{value:int}")]
public string GetProducts(int value)
{
var json = "";
var products = db.Products;
if (products.Any())
{
foreach (var s in products)
{
ProductApi product = new ProductApi();
product.Name = s.Name;
json += JsonConvert.SerializeObject(supplier);
}
}
var status = db.Status;
if (status.Any())
{
json += "{status:1}";
}
else
{
json += "{status:0}";
}
return json;
}
public class ProductApi
{
public string Name { get; set; }
}
另外,这个output/response有效吗?
[
{
"id":1,
"name":"product name"
},
{
"id":2,
"name":"product name 2"
},
{
"id":3,
"name":"product name 3"
}
]
{
"status": 0
}
正如 raderick 提到的,您不需要创建自己的自定义 JSON 基础架构。
public class ProductApi
{
public int Id {get;set;}
public string Name { get; set; }
}
public class ResponseDTO
{
public int Status {get;set;}
public List<ProductApi> { get; set; }
}
并且在您的 API 操作中,return 像这样:
[Route("{value:int}")]
public ResponseDTO GetProducts(int value)
{
ResponseDTO result = ...// construct response here
return result;
}
下面是 post 的变化:
首先,当你传递一个text/html
请求时,你应该默认你的apireturnJson(这是你要找的吗?),添加这个行到您的 WebApiConfig
class:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
其次,我将代码更改为return一个真实的对象,冒充您的回复:
public class ProductApiCollection
{
public ProductApi[] Products { get; set; }
public byte Status { get; set; }
}
public class ProductApi
{
public string Name { get; set; }
}
方法体:
public ProductApiCollection Get()
{
var result = new ProductApiCollection();
var dbProducts = db.Products;
var apiModels = dbProducts.Select(x => new ProductApi { Name = x.Name } ).ToArray();
result.Products = apiModels;
var status = db.Status.Any() ? 1 : 0;
result.Status = status;
return result;
}
这将导致以下示例 json:
{
"Products": [
{
"Name": "Pork"
},
{
"Name": "Beef"
},
{
"Name": "Chicken"
},
{
"Name": "Salad"
}
],
"Status": 1
}
我强烈建议您不要对此类内容进行手动格式化,而要依赖内置库和第 3 方库。否则,您将重新发明已经可用、经过测试并准备好工作的东西。