.net core web api 返回json数据格式
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍一种方法
1.先创建一个实体类
public partial class TBook
{
public int Id { get; set; }
public string Bookname { get; set; }
public DateTime DateTime { get; set; }
public string CreatName { get; set; }
}
2.然后创建上下文类
3.编写一个返回数据的返回类APIModle
任何接口都应该返回int status、object data、string msg这三个共同的属性。前端根据状态值做不同的业务逻辑,msg用于在一些情况直接展示接口返回的文本进行提示而不必在前端写死。所以,我们需要要定义一个类,所有接口的数据都必须是这个类。
public class ApiModel
{
//业务状态 可以是 1.成功 2.失败等等
public int status { get; set; }
//业务数据
public object data { get; set; }
//提示信息
public String msg { get; set; }
}
4.然后我们写一个测试数据的接口
[HttpGet]
public ApiModel Get()
{
var list = VipContext.TBooks.ToList();
return new ApiModel()
{
status = 1,
//转换为json数据格式
data = JsonConvert.SerializeObject(list),
//不转换json数据格式
//data = list,
msg ="查询成功"
};
}
这里有一个坑 如果你转换Json数据格式的话反而得到一个不太好得数据形式 下面大家看看这两种情况对比
这是转换json格式的
为什么会是个字符串,这是因为我们多余的为对象进行序列化导致的!
这是没转换的
所以我们要用要采用正确的方式
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 天前
更多推荐
已为社区贡献3条内容
所有评论(0)