asp.net core json解析

浪淘沙1周前后端9

一、json

{
  "cover": {
    "title": "2",
    "img": "https://localhost:5001/sysadmins/ts/2",
    "link": "2",
    "url": "ts_nr"
  },
  "list": [
    {
      "title": "3",
      "img": "https://localhost:5001/sysadmins/ts/3",
      "link": "3",
      "url": "ts_nr"
    },  {
      "title": "4",
      "img": "https://localhost:5001/sysadmins/ts/4",
      "link": "4",
      "url": "ts_nr"
    }
  ]
}


2、解析

  using var doc = JsonDocument.Parse(tsnr);

  var coverTitle = doc.RootElement
      .GetProperty("cover")
      .GetProperty("title")
      .GetString();
  var img = doc.RootElement
     .GetProperty("cover")
     .GetProperty("img")
     .GetString();
  var link = doc.RootElement
     .GetProperty("cover")
     .GetProperty("link")
     .GetString();

3、数组长度

var list = doc.RootElement.GetProperty("list");
int count = list.GetArrayLength();

4、数组遍历

 foreach (var item in doc.RootElement.GetProperty("list").EnumerateArray())
 {
     var _title = item.GetProperty("title").GetString();
     var _img = item.GetProperty("img").GetString();
     var _link = item.GetProperty("link").GetString();
}


相关文章

asp.net core razor 读取form中,同一name的值 分解

asp.net core razor 读取form中,同一name的值 分解

@page @model xxtsoft.Web.Entry.Pages.sysadmins.mb.mbModel @{     Layout&...

asp.net core Razor 部门视图Partial Views 用法

1、在/Pages/Shared/目录下创建设视图文件 _top.cshtml2、在razor页面中进行引用@await Html.PartialAsync("_top&...

点击按钮,禁用提示,加导出word

@page @model xxtsoft.Web.Entry.Pages.sysadmins.kh.kh_outModel @{     Lay...

asp.net core 发送模板消息

 private readonly HttpClient _httpClient;  public testModel(HttpCli...

短信发送

@page @model xxtsoft.Web.Entry.Pages.Index1Model @{ } <form method="post"...

asp.net core指定运行端口 如:http://localhost:5002

网站根目录新建:hosting.json输入:{   "urls": "http://localhost:5002" }...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。