2

OutPut:

{
  "$id": "1",
  "_MenuList": [
    {
      "$id": "2",
      "parentId": 3,
      "menuName": "Details"
    },
    {
      "$id": "3",
      "parentId": 1,
      "menuName": "No of Details"
    }
  ]
}

In output "$id" autogenerate how to remove this.

Format Code Controller :

 [Route("MenuController/getMenuList/input")]
    [HttpPost]
    public ReportMenu getMenuList([FromBody]LoginModel input)
    {
        DataTable reportvalue = new DataTable();
        try
        {
            using (SqlConnection dataConnection = ConnectionString.getConnection())
            {
                SqlCommand command = new SqlCommand("USP_ReportMenu", dataConnection);
                command.Parameters.AddWithValue("@userName", input.userName);
                command.Parameters.AddWithValue("@password", input.password);
                command.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter reportAdapter = new SqlDataAdapter(command))
                {

                    reportAdapter.Fill(reportvalue);

                }
            }
        }
        catch (SqlException ex)
        {

        }
        return getJson(reportvalue);

    }

public ReportMenu getJson(DataTable tb)
{
        ReportMenu menu = new ReportMenu();
        List<MenuModel> listMenu = new List<MenuModel>();
        for (int i = 0; i < tb.Rows.Count; i++)
        {
            MenuModel obj=new MenuModel();
            obj.parentId = Convert.ToInt16(tb.Rows[i]["parentId"].ToString());
            obj.menuName = tb.Rows[i]["reportName"].ToString();
            listMenu.Add(obj);


        }

        menu._MenuList = listMenu;
        return menu;
    }

I want To this Type of ouput:

{"_MenuList": [
    {
      "parentId": 3,
      "menuName": "Details"
    },
    {
      "parentId": 1,
      "menuName": "No of Details"
    }
  ]
}

Kindly help for us.. I am new in Web API.

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Hemina
  • 375
  • 1
  • 11
  • Post your related code.... There is nothing about json here – L.B Sep 12 '16 at 11:13
  • i had done in controller class which i done – Hemina Sep 12 '16 at 11:16
  • 1
    This looks like your json serializer settings, without the code it's difficult to say: but check this out: [http://stackoverflow.com/a/23461179/4045532](http://stackoverflow.com/a/23461179/4045532) – Corporalis Sep 12 '16 at 11:41
  • I use JsonSerilizer class but auto generate $id which i don't requried. – Hemina Sep 12 '16 at 12:23
  • Can you provide the code where you setup your serializer? – Corporalis Sep 12 '16 at 12:41
  • In webApicofiguration.cs use var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); use this code – Hemina Sep 12 '16 at 12:44

1 Answers1

2

Based on the comments, what you'll need to do (as per the article I added in the comments) is replace:

json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

With this:

json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

That will then ignore any reference handling, but you will get your desired result.

Corporalis
  • 1,032
  • 1
  • 9
  • 17