• <tfoot id='9ZkKu'></tfoot>
    <i id='9ZkKu'><tr id='9ZkKu'><dt id='9ZkKu'><q id='9ZkKu'><span id='9ZkKu'><b id='9ZkKu'><form id='9ZkKu'><ins id='9ZkKu'></ins><ul id='9ZkKu'></ul><sub id='9ZkKu'></sub></form><legend id='9ZkKu'></legend><bdo id='9ZkKu'><pre id='9ZkKu'><center id='9ZkKu'></center></pre></bdo></b><th id='9ZkKu'></th></span></q></dt></tr></i><div id='9ZkKu'><tfoot id='9ZkKu'></tfoot><dl id='9ZkKu'><fieldset id='9ZkKu'></fieldset></dl></div>

        <legend id='9ZkKu'><style id='9ZkKu'><dir id='9ZkKu'><q id='9ZkKu'></q></dir></style></legend>
      1. <small id='9ZkKu'></small><noframes id='9ZkKu'>

        • <bdo id='9ZkKu'></bdo><ul id='9ZkKu'></ul>

        如何使用 json.net 将 c# 通用列表转换为 json?

        时间:2023-08-23
          <tbody id='fUMIO'></tbody>

          <bdo id='fUMIO'></bdo><ul id='fUMIO'></ul>
            • <tfoot id='fUMIO'></tfoot>

              1. <small id='fUMIO'></small><noframes id='fUMIO'>

                • <i id='fUMIO'><tr id='fUMIO'><dt id='fUMIO'><q id='fUMIO'><span id='fUMIO'><b id='fUMIO'><form id='fUMIO'><ins id='fUMIO'></ins><ul id='fUMIO'></ul><sub id='fUMIO'></sub></form><legend id='fUMIO'></legend><bdo id='fUMIO'><pre id='fUMIO'><center id='fUMIO'></center></pre></bdo></b><th id='fUMIO'></th></span></q></dt></tr></i><div id='fUMIO'><tfoot id='fUMIO'></tfoot><dl id='fUMIO'><fieldset id='fUMIO'></fieldset></dl></div>
                  <legend id='fUMIO'><style id='fUMIO'><dir id='fUMIO'><q id='fUMIO'></q></dir></style></legend>
                  本文介绍了如何使用 json.net 将 c# 通用列表转换为 json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在将我的数据表转换为 c# 通用列表.

                  I am converting my datatable to c# generic list.

                   DataTable dt = mydata();
                   List<DataRow> list = dt.AsEnumerable().ToList();
                  

                  现在如何使用 json.net 将此 list 转换为 json?任何建议.

                  Now how can i convert this list to json using json.net? Any suggestion.

                  json格式的样本应该是这样的,

                  Sample of json format should be like this,

                  {"Table" : [{"userid" : "1","name" : "xavyTechnologies","designation" : "",
                  "phone" : "9999999999","email" : "test@test.com","role" : "Admin","empId" : "",
                   "reportingto" : ""},{"userid" : "2","name" : "chendurpandian","designation" :
                   "softwaredeveloper","phone" : "9566643707","email" : "chendur.pandia@gmail.com",
                   "role" : "Super User","empId" : "1","reportingto" : "xavyTechnologies"},
                  {"userid" : "3","name" : "sabarinathan","designation" : "marketer","phone" :
                  "66666666666","email" : "bala3569@gmail.com","role" : "User",
                   "empId" : "2","reportingto" : "chendurpandian"}]}
                  

                  推荐答案

                  这是一个例子:

                  using System;
                  using System.Data;
                  using Newtonsoft.Json.Linq;
                  
                  class Test
                  {
                      static void Main()
                      {
                          DataTable table = new DataTable();
                          table.Columns.Add("userid");
                          table.Columns.Add("phone");
                          table.Columns.Add("email");
                  
                          table.Rows.Add(new[] { "1", "9999999", "test@test.com" });
                          table.Rows.Add(new[] { "2", "1234567", "foo@test.com" });
                          table.Rows.Add(new[] { "3", "7654321", "bar@test.com" });
                  
                          var query = from row in table.AsEnumerable()
                                      select new {
                                          userid = (string) row["userid"],
                                          phone = (string) row["phone"],
                                          email = (string) row["email"]            
                                      };
                  
                          JObject o = JObject.FromObject(new
                          {
                              Table = query
                          });
                  
                          Console.WriteLine(o);
                      }
                  }
                  

                  文档:LINQ to JSON使用 Json.NET

                  这篇关于如何使用 json.net 将 c# 通用列表转换为 json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 JSON.NET 序列化/反序列化对象字典 下一篇:将 JSON 递归反序列化为 IDictionary&lt;string,obje

                  相关文章

                  最新文章

                • <i id='G1EnR'><tr id='G1EnR'><dt id='G1EnR'><q id='G1EnR'><span id='G1EnR'><b id='G1EnR'><form id='G1EnR'><ins id='G1EnR'></ins><ul id='G1EnR'></ul><sub id='G1EnR'></sub></form><legend id='G1EnR'></legend><bdo id='G1EnR'><pre id='G1EnR'><center id='G1EnR'></center></pre></bdo></b><th id='G1EnR'></th></span></q></dt></tr></i><div id='G1EnR'><tfoot id='G1EnR'></tfoot><dl id='G1EnR'><fieldset id='G1EnR'></fieldset></dl></div>
                    <bdo id='G1EnR'></bdo><ul id='G1EnR'></ul>
                  1. <legend id='G1EnR'><style id='G1EnR'><dir id='G1EnR'><q id='G1EnR'></q></dir></style></legend>
                  2. <small id='G1EnR'></small><noframes id='G1EnR'>

                    <tfoot id='G1EnR'></tfoot>