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.

6
  • Post your related code.... There is nothing about json here Commented Sep 12, 2016 at 11:13
  • i had done in controller class which i done Commented Sep 12, 2016 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 Commented Sep 12, 2016 at 11:41
  • I use JsonSerilizer class but auto generate $id which i don't requried. Commented Sep 12, 2016 at 12:23
  • Can you provide the code where you setup your serializer? Commented Sep 12, 2016 at 12:41

1 Answer 1

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.

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.