【问题标题】:Add custom name to an array using JSON.NET使用 JSON.NET 将自定义名称添加到数组
【发布时间】:2015-07-21 02:16:12
【问题描述】:

我需要像这样检索 JSON 对象的数组:

{ "MyCustomName": [ { "id": 0, "item": "item 0" }, { "id": 1, "item": "item 1" } ] }

而不是这个:

{ [ { "id": 0, "item": "item 0" }, { "id": 1, "item": "item 1" } ] }

这是我使用的代码:

    using (SqlConnection con = conection.Conect())
            {
                using (SqlCommand cmd = new SqlCommand("SelPeople", con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable("MyCustomName");  
                        sda.Fill(dt);
                        List<Personal> ListOfPeople= new List<Personal>();

                        foreach (DataRow dr in dt.Rows)
                        {
                            Personal persona = new Personal();

                            persona.Id = Convert.ToInt32(dr["Id"].ToString());
                        persona.Name = dr["Name"].ToString();
                        //add one row to the list
                        ListOfPeople.Add(persona);
                    }

                   JsonConvert.SerializeObject(ListOfPeople, Formatting.Indented);
                    Response.Write(json);
                }
            }
        }

一些帮助会很好,谢谢:)

【问题讨论】:

    标签: c# arrays list datatable json.net


    【解决方案1】:

    看这个帖子from another user

    在你的控制器中改变这部分

    JsonConvert.SerializeObject(ListOfPeople, Formatting.Indented);
    Response.Write(json);
    

    收件人:

    string json = JsonConvert.SerializeObject(new
    {
        MyCustomName = ListadoDePersonal;
    });
    Response.Write(json);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2014-08-02
      相关资源
      最近更新 更多