asp.net mvc json结果格式

发布于 2024-08-27 00:23:08 字数 1987 浏览 6 评论 0原文

public class JsonCategoriesDisplay
    {
        public JsonCategoriesDisplay() { }

        public int CategoryID { set; get; }
        public string CategoryTitle { set; get; }
    }

    public class ArticleCategoryRepository
    {
        private DB db = new DB();  

        public IQueryable<JsonCategoriesDisplay> JsonFindAllCategories()
        {
            var result = from c in db.ArticleCategories                         
                         select new JsonCategoriesDisplay
                         {
                             CategoryID = c.CategoryID,
                             CategoryTitle = c.Title                            
                         };

            return result;
        }

          ....
    }

public class ArticleController : Controller
    {
        ArticleRepository articleRepository = new ArticleRepository();
        ArticleCategoryRepository articleCategoryRepository = new ArticleCategoryRepository();


        public string Categories()
        {
            var jsonCats = articleCategoryRepository.JsonFindAllCategories().ToList();

            //return Json(jsonCats, JsonRequestBehavior.AllowGet);

            return new JavaScriptSerializer().Serialize(new { jsonCats});
        }

    }

结果如下:

{"jsonCats":[{"CategoryID":2,"CategoryTitle":"Politika"},{"CategoryID":3,"CategoryTitle":"Informatika"},{"CategoryID":4,"CategoryTitle":"Nova
kategorija"},{"CategoryID":5,"CategoryTitle":"Testna
kategorija"}]}

如果我使用注释行并放置 JsonResult 而不是返回字符串,我会得到以下结果:<

[{"CategoryID":2,"CategoryTitle":"Politika"},{"CategoryID":3,"CategoryTitle":"Informatika"},{"CategoryID":4,"CategoryTitle":"Nova
kategorija"},{"CategoryID":5,"CategoryTitle":"Testna
kategorija"}]

但是,我需要将结果格式化为如下所示:

{'2':'Politika','3':'Informatika','4':'Nova
kateorija','5':'Testna kategorija'}

是否有一种简单的方法来完成此操作,或者我需要对结果进行硬编码?

public class JsonCategoriesDisplay
    {
        public JsonCategoriesDisplay() { }

        public int CategoryID { set; get; }
        public string CategoryTitle { set; get; }
    }

    public class ArticleCategoryRepository
    {
        private DB db = new DB();  

        public IQueryable<JsonCategoriesDisplay> JsonFindAllCategories()
        {
            var result = from c in db.ArticleCategories                         
                         select new JsonCategoriesDisplay
                         {
                             CategoryID = c.CategoryID,
                             CategoryTitle = c.Title                            
                         };

            return result;
        }

          ....
    }

public class ArticleController : Controller
    {
        ArticleRepository articleRepository = new ArticleRepository();
        ArticleCategoryRepository articleCategoryRepository = new ArticleCategoryRepository();


        public string Categories()
        {
            var jsonCats = articleCategoryRepository.JsonFindAllCategories().ToList();

            //return Json(jsonCats, JsonRequestBehavior.AllowGet);

            return new JavaScriptSerializer().Serialize(new { jsonCats});
        }

    }

This results with following:

{"jsonCats":[{"CategoryID":2,"CategoryTitle":"Politika"},{"CategoryID":3,"CategoryTitle":"Informatika"},{"CategoryID":4,"CategoryTitle":"Nova
kategorija"},{"CategoryID":5,"CategoryTitle":"Testna
kategorija"}]}

If I use line that is commented and place JsonResult instead of returning string, I get following result:<

[{"CategoryID":2,"CategoryTitle":"Politika"},{"CategoryID":3,"CategoryTitle":"Informatika"},{"CategoryID":4,"CategoryTitle":"Nova
kategorija"},{"CategoryID":5,"CategoryTitle":"Testna
kategorija"}]

But, I need result to be formatted like this:

{'2':'Politika','3':'Informatika','4':'Nova
kateorija','5':'Testna kategorija'}

Is there a simple way to accomplish this or I will need to hardcode result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风夜微凉 2024-09-03 00:23:08

查看 Json.Net 库

Json.NET 库可以正常工作
具有 JavaScript 和 JSON 格式
.NET 中的数据很简单。快速阅读并
使用 JsonReader 编写 JSON 并
JsonWriter 或序列化您的 .NET
具有单个方法调用的对象
使用 JsonSerializer。

我已在 .net mvc 项目中成功使用它。

Have a look at the Json.Net library

The Json.NET library makes working
with JavaScript and JSON formatted
data in .NET simple. Quickly read and
write JSON using the JsonReader and
JsonWriter or serialize your .NET
objects with a single method call
using the JsonSerializer.

I have successfully used it within a .net mvc project.

耶耶耶 2024-09-03 00:23:08

ile,

你真的应该看看 json.net lib,我最近在 json/jquery 和 mvc 方面遇到了类似的困境,并尝试了我自己的手卷版本,但在我自己的实现中遇到了许多限制。 newton json lib 非常简单 - 使用简单并且始终在更新。当前版本可以很好地处理嵌套结构,从而使特定的 json 格式问题不再是问题。

看一下,您需要 15 分钟才能掌握。

ile,

you really should look at the json.net lib, i was in a similar dilemma recently with json/jquery and mvc and tried my own hand-rolled version but hit the many limitations in my own implemetation. the newton json lib is quite simply a no-brainer - simple to use and always being updated. the current version works fantastically with nested structures, thus making that particular json formatting issue a non-issue.

give it a look, will take you 15 mins to get to grips with.

只是我以为 2024-09-03 00:23:08

尝试将其作为数组返回,另外,如果您返回 JsonResult 您可以节省大量代码(看起来您正在尝试获取所需的格式),

public class ArticleController : Controller
    {
        ArticleRepository articleRepository = new ArticleRepository();
        ArticleCategoryRepository articleCategoryRepository = new ArticleCategoryRepository();

    public JsonResult Categories()
    {
        var jsonCats = articleCategoryRepository.JsonFindAllCategories().ToArray();

        return Json(jsonCats, JsonRequestBehavior.AllowGet);
    }

}

在我的测试中我得到了结果

[{"Id":1,"Name":"Cat 1"},{"Id":2,"Name":"Cat 2"}]

Try returning it as an Array, Also if you return a JsonResult you can save alot of code (looks like you were attempting to get the formatting you wanted)

public class ArticleController : Controller
    {
        ArticleRepository articleRepository = new ArticleRepository();
        ArticleCategoryRepository articleCategoryRepository = new ArticleCategoryRepository();

    public JsonResult Categories()
    {
        var jsonCats = articleCategoryRepository.JsonFindAllCategories().ToArray();

        return Json(jsonCats, JsonRequestBehavior.AllowGet);
    }

}

in my test I got a result of

[{"Id":1,"Name":"Cat 1"},{"Id":2,"Name":"Cat 2"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文