Convert Object to JSON in MVC 4

前端 未结 3 1959
被撕碎了的回忆
被撕碎了的回忆 2021-02-03 21:30

I am converting an object to JSON using JavaScriptSerializer and I can see this JSON output in server code:

[{\"UserId\":1,\"UserName\":\"Admin\"}]
         


        
3条回答
  •  忘掉有多难
    2021-02-03 22:12

    Why are you doing that? Why not just return a JsonResult?

    public ActionResult MyMethod()
    {
        List list = new List() {
            new ListItem() { UserId = "1", UserName = "Admin" },
            new ListItem() { UserId = "2", UserName = "JohnDoe" },
            new ListItem() { UserId = "3", UserName = "JaneDoe" }};
    
        return this.Json(list);
    }
    

提交回复
热议问题