2

I need to retrive the array of JSON object like this:

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

instead of this:

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

Here is the code i've use:

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

Some help would be nice, thanks :)

1 Answer 1

3

Look at this post from another user

In your controller change this part

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

To:

string json = JsonConvert.SerializeObject(new
{
    MyCustomName = ListadoDePersonal;
});
Response.Write(json);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.