使用JSON.NET向数组添加自定义名称



我需要像这样检索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);
                }
            }
        }

如果能帮忙就好了,谢谢:)

看看另一个用户的帖子

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

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

:

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

相关内容

  • 没有找到相关文章

最新更新