更优雅的方式将json数组返回到ASP.NET MVC

时间:2022-10-28 04:05:01

{Sorry new to JSON} I need to build up an array of resources (Users) and pass it in to my view, might be a better way than what ive done below? (Demo)

{对不起JSON新手}我需要建立一个资源数组(用户)并将其传递给我的视图,这可能比我在下面做的更好吗? (演示)

My model is simply

我的模型很简单

public class ScheduleUsers
    {
        public string Resource{ get; set; }
}

On my controller

在我的控制器上

var users = new JsonArray(
               new JsonObject(
                new KeyValuePair<string,JsonValue>("id","1"),
                new KeyValuePair<string,JsonValue>("name","User1")),
                new JsonObject(
                new KeyValuePair<string, JsonValue>("id", "2"),
                new KeyValuePair<string, JsonValue>("name", "User2"))
                  );
            model.Resources = users.ToString();

2 个解决方案

#1


13  

Why don't you just return a list of entities as a JSON result, like:

为什么不直接返回实体列表作为JSON结果,如:

public class CarsController : Controller  
{  
    public JsonResult GetCars()  
    {  
        List<Car> cars = new List<Car>();
        // add cars to the cars collection 
        return this.Json(cars, JsonRequestBehavior.AllowGet);  
    }  
} 

It will be converted to JSON automatically.

它将自动转换为JSON。

#2


2  

I did this and this works

我这样做了,这很有效

    JavaScriptSerializer js = new JavaScriptSerializer();
                StringBuilder sb = new StringBuilder();
                //Serialize  
                js.Serialize(GetResources(), sb);



 public List<ScheduledResource> GetResources()
        {
            var res = new List<ScheduledResource>()
                {
                    new ScheduledResource()
                        {
                            id = "1",
                            color = "blue",
                            name = "User 1"
                        },
                    new ScheduledResource()
                        {
                            id = "2",
                            color = "black",
                            name = "User 2"
                        },

                };

            return res;
        }

#1


13  

Why don't you just return a list of entities as a JSON result, like:

为什么不直接返回实体列表作为JSON结果,如:

public class CarsController : Controller  
{  
    public JsonResult GetCars()  
    {  
        List<Car> cars = new List<Car>();
        // add cars to the cars collection 
        return this.Json(cars, JsonRequestBehavior.AllowGet);  
    }  
} 

It will be converted to JSON automatically.

它将自动转换为JSON。

#2


2  

I did this and this works

我这样做了,这很有效

    JavaScriptSerializer js = new JavaScriptSerializer();
                StringBuilder sb = new StringBuilder();
                //Serialize  
                js.Serialize(GetResources(), sb);



 public List<ScheduledResource> GetResources()
        {
            var res = new List<ScheduledResource>()
                {
                    new ScheduledResource()
                        {
                            id = "1",
                            color = "blue",
                            name = "User 1"
                        },
                    new ScheduledResource()
                        {
                            id = "2",
                            color = "black",
                            name = "User 2"
                        },

                };

            return res;
        }