How can I change the asp.net mvc JSON formatting method?

I have this model :

public class Person { public string Name { get; set; } public int Age { get; set; } public List<Color> Colors { get; set; } } public class Color { public int ColorId { get; set; } public string Name { get; set; } } 

and asp.net MVC return Json(...) gives me the following:

 [{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]}, {"Name":"Albert","Age":29,"Colors":[{"ColorId":2,"Name":"Blue"}]}] 

when i try to return type: List<Person>

but I want something like this (if possible):

 {"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},{"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]} 

MY QUESTION (S):

  • How can I get C # (asp.net mvc) to return JSON with a better format of something like: (note: ignore the data, my main task is to return with β€œpeople” as the main collection .. how do I do this? JSON. net?)

    {"People": [{"Name": "JC", "Age": 24, "Colors": [{"ColorId": 1, "Name": "Red"}, {"ColorId": 2, " Name ":" Blue "}]}, {" Name ":" Albert "," Age ": 83," Colors ": [{" ColorId ": 2," Name ":" Blue "}]}]}}

  • OR how can I get KNOCKOUT.JS MAPPING PLUGIN to work with this type of JSON format? (for those who know knockout)

    [{"Name": "JC", "Age": 24, "Colors": [{"ColorId": 1, "Name": "Red"}, {"ColorId": 2, "Name": "Blue "}]}, {" Name ":" Albert "," Age ": 29," Colors ": [{" ColorId ": 2," Name ":" Blue "}]}]]


UPDATE (additional clarification / information):

this is my data and I want to return the list

  private List<Person> _people = new List<Person> { new Person { Name = "JC", Age = 24, Colors = new List<Color> { Red, Blue, } }, new Person { Name = "Albert", Age = 29, Colors = new List<Color> { Blue } } }; 

in JSON format like this:

 {"People":[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]}, {"Name":"Albert","Age":83,"Colors":[{"ColorId":2,"Name":"Blue"}]}]} 

I'm just wondering if this is possible, or if not , how can I make the knockout.js mapping plugin adapt to the MVC json return method?

0
source share
2 answers

You need a container, since you do not want to return an array, but an object with the People variable.

Something like this (using dynamic):

 var jsonData = new { People = _people }; return Json(jsonData, JsonRequestBehavior.AllowGet); 

Update

JSON is a really simple format. Let me skip all that you do not need now.

  • Objects: Objects in json start and end with {} . Everything that matches the properties in C # in them.
  • Arrays: returning IEnumerable will return an array. An array may contain other arrays, objects, or simple fields.

The above code uses a dynamic object in C # and it can be translated into a class that looks like this:

 public class MyCustomClass { public IEnumerable<Person> People {get;set;} } 

Therefore, this is an object that returns an array, resulting in:

 { People: [] } 

Where {} matches MyCustomClass .

+3
source

you can return things, for example, as follows:

  var jsonData = new { Name = qry.Name, Age = qry.Age, Colors = ( from c in qry select new { ColorID = c.ColorID, Name = c.Name }).ToArray() }; return Json(jsonData, JsonRequestBehavior.AllowGet); 

Maybe something like this :)

+1
source

Source: https://habr.com/ru/post/1388431/


All Articles