Peru
Peru

Reputation: 2971

To add a JSON Array inside a JSON object in C#

Am working in MVC. In my controller am creating a list

 public class GridColModelLst
    {
        List<GridColModel> _items = new List<GridColModel>();

        public List<GridColModel> items
        {
            get { return _items; }
            set { _items = value; }
        }
    }


    public class GridColModel
        {
            public string name { get; set; }
            public int? width { get; set; }
    }

And passing this list as JSON which works fine and my JSON is below

[{ name: 'Humidity', width: 90}]

How do i make it like the below JSON To add a JSON Array inside a JSON object in C#

[{ name: 'Humidity', width: 90, searchoptions: { sopt: ['eq', 'ne'] } }]

Thanks

====================

update

In my controller am sending the list like

return Json(Colmodel, JsonRequestBehavior.AllowGet);

and am getting [{ name: 'Humidity', width: 90}] succesfully

What changes i need for the class to get a JSON like

[{ name: 'Humidity', width: 90, searchoptions: { sopt: ['eq', 'ne'] } }]

Upvotes: 0

Views: 2626

Answers (1)

TGH
TGH

Reputation: 39278

Try this:

 return Json(new { name = "Humidity", width = 90, searchoptions = new { sopt = new string[] { "eq,nq" } } }, JsonRequestBehavior.AllowGet);

Upvotes: 1

Related Questions