如何从处理法Array属性返回JSON? [英] How do I return Json from Action Method with Array property?

查看:170
本文介绍了如何从处理法Array属性返回JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为JSON从操作方法返回一些数据。

I am trying to return some data as json from an action method.

我有一个员工对象,看起来像这样:

I have an employee object that looks like this:

public class Employee
{
     public int EmployeeID {get; set;}
     public string FirstName {get; set;}
     public string LastName {get; set;}
     //Other irrelevant properties
}

然后,我有一个视图模型如下:

Then I have a view model as follows

public Class EmployeeViewModel
{

    public Employee Supervisor{get; set;}
    public List<EmployeeViewModel> Employees

} 

我需要返回一个JSON对象,它看起来就像这样:

I need to return a json object that looks exactly like this:

{id: 1, name: John, children: [
    {id: 1, name: Joe, children: []},
    {id: 1, name: Rob, children: []}
]}

现在我只需要去第二层如上,返回,监事及其工作人员在他们之下。

For now I only need to go to the second level as above, returning and supervisor and their staff members beneath them.

我怎么会去我的操作方法返回这个(我有视图模型对象水合已经我只需要它返回的JSON)。我的问题,到目前为止一直是在孩子属性没有得到填充。

How would I go about returning this in my action method(I have the viewModel object hydrated already I just need to return it as json). My problem so far has been that the children property does not get populated.

推荐答案

您将如何简单,这是赞叹不已。只要设置你的模型对象,让他们有正确的属性。随着你努力实现JSON字符串,你不希望一个视图模式,因为你似乎想顶层对象包含员工的属性。而如果你使用一个视图模型对象,然后顶级对象将是视图模型对象,和员工将是对象的属性。

You will be amazed at how simple this is. Just set up your model objects so that they have the correct properties. With the JSON string you are trying to achieve, you don't want a view model because you appear to want the top level object to contain properties of employee. Whereas if you use a viewmodel object then the top level object will be the view model object, and employee will be a property of that object.

相反,我想你想的顶级对象是与儿童是(子对象)的列表属性的员工。子对象也有一个名为儿童属性。这可能是递归填充。构建目标是你的责任,但我确实提供了一些伪code。与伪数据,让你开始。

Instead I think you want the top level object to be an employee with a property that is a list of children (child objects). Child objects also have a property called children. Which could be populated recursively. Building the object is your responsibility, but I did provide some pseudo-code with pseudo-data to get you started.

public Class Employee
{
     public int EmployeeID {get; set;}
     public string FirstName {get; set;}
     public string LastName {get; set;}
     public List<Child> Children {get; set;}
}

public Class Child
{
    public int ChildID {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public List<Child> Children {get; set;{
}

现在您的操作方法。你知道你可以只返回JsonResult?做到这一点:

Now your action method. Did you know you could just return JsonResult? Do this:

public JsonResult GetJSON()
{
    //Build employee object and add children to list. Something like the following pseudo-code:
    List<Child> childrenList = new List<Child>
    {
        new Child
        {
            ChildID = //some id,
            FirstName = "Joe",
            LastName = "Smith"
            // Add children to list.
        },
        // Add more children to list.
    }
    Employee emp = new Employee
    {
        EmployeeID = 123,
        FirstName = "John",
        LastName = "Doe",
        Children = childrenList
    };
    Return Json(emp);
}

这JSON看起来像:

{ "EmployeeID":"123", "FirstName":"John", "LastName":"Doe", "Children":[
    { "ChildID":"someid", "FirstName":"Joe", "LastName":"Smith", Children [] },
    { etc... }
] }

如果您不熟悉使用JSON很多,那么你应该知道,引号都变量和名称确保没有什么是PTED misinter $ P $。 MVC会为你和相当不错的。它将把员工变成一个JSON数组列表属性没有任何工作,你的一部分,它会整齐,可读性格式化JSON。

If you are not familiar with JSON much then you should know that quotation marks around both the variable and the name ensure that nothing is misinterpreted. MVC does it for you, and quite well. It will turn the list property of employee into a JSON array without any work on your part and it will format the JSON neatly and readably.

现在使用MVC 3,你可以绑定模型到JSON也。如果你与一个JSON对象的请求包括,MVC 3会自动绑定它的属性给雇员的对象。所以,如果你贴我上面列出的相同的JSON字符串,你的操作方法可能看起来是这样的:

Now with MVC 3 you can model-bind to JSON also. If you make a request with a json object included, MVC 3 will automatically bind it's properties to an employee object. So if you posted the same JSON string that I listed above, your action method could look like this:

public ActionResult SampleMethod(Employee emp)
{
    //emp would be the same object you sent as JSON earlier and are now sending back :D
}

注意:您可能会考虑更改员工,并给它一个物业名为孩子类型列表与LT;人&GT; 。这样,你会在员工儿童类合并成一个。但是,我自然不能看到所有的code,所以也许你有足够的独特性能,值得两班。

NOTE: You might consider changing Employee to Person, and giving it a property called Children of type List<Person>. That way you would be combining the Employee and Child classes into one. But naturally I can't see all your code, so maybe you do have enough unique properties to merit the two classes.

这篇关于如何从处理法Array属性返回JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆