2015-07-21 196 views
0

我需要以檢索JSON對象的像這樣的數組的數組:添加自定義名稱使用JSON.NET

{ "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); 
       } 
      } 
     } 

一些幫助就好了,謝謝:)

回答

1

看看這個帖子from another user

在你的控制人變更這一部分

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

要:

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