2

我正在尝试为返回的 JSON 获得更好的格式 - 但遗憾的是,我似乎只能得到一个长字符串 - 即使很难我也可以反序列化它,我希望它更具可读性以进行调试。

这就是我到目前为止所拥有的:

    public JsonResult Get(string recipeId)
    {
        int id = Convert.ToUInt16(recipeId);
        Recipe recipe = recipeRepo.Recipes.Include("Pairings").Include("UsedIngredients").Include("RecipeImages").Include("Tags").Where(r => r.RecipeId == id).FirstOrDefault();

        var result = new JsonResult();
        var data = JsonConvert.SerializeObject(recipe, Formatting.Indented, new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                NullValueHandling = NullValueHandling.Ignore
            });

        result.Data = data;
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        return result;

    }

这就是返回的内容:

"{\r\n \"$id\": \"1\",\r\n \"RecipeId\": 1004,\r\n \"Name\": \"全麦香肠意大利面\ u0026 Peppers\",\r\n \"Serving\": 4,\r\n \"说明\": \"\u003cp\u003e\u003cspan style=\\"font-size:16px\\"\u003e \u003cspan 样式=\\"字体系列:格鲁吉亚,衬线\\"\u003e1. 在一个大煎锅中,用中火加热油。加入香肠并煮,转动,直到变成褐色并煮熟,大约 8 分钟。消除。当香肠凉到可以处理时,将其切成 1/2 英寸的切片。\u003c/span\u003e\u003c/span\u003e\u003c/p\u003e\r\n\r\n\u003cp\u003e\ u003cspan 样式=\\"字体大小:16px\\"\u003e\u003cspan 样式=\\"字体家族:格鲁吉亚,衬线\\"\u003e2。在一大锅沸腾的盐水中,把意大利面煮熟,干的大约 12 分钟,新鲜的大约 2 分钟。\u0026nbsp;\u003c/span\u003e\u003c/span\u003e\u003c/p\u003e\r\n\r\n\u003cp\ u003e\u003cspan style=\\"font-size:16px\\"\u003e\u003cspan style=\\"font-family:georgia,serif\\"\u003e3. 当意大利面煮熟时,在锅中加入洋葱、辣椒和 3/4 茶匙盐。煮,经常搅拌,直到蔬菜开始变褐色,大约 5 分钟。盖上盖子煮,偶尔搅拌直到蔬菜变软,大约多 3 分钟。加入大蒜,搅拌,煮约 30 秒。加入西红柿、肉汤、苦艾酒、保留的香肠和任何积累的汁液、欧芹和剩余的 1 茶匙盐,然后用文火炖。\u003c/span\u003e\u003c/span\u003e\u003c/p\u003e\ r\n\",\r\n \"准备时间\": 10,http://www.clubw.com/pc/2581789/ \",\r\n \"Description\": \"我们不能再袖手旁观,当人们生产出如此多的好例子时,包括这酒。李子和黑莓的味道使它成为搭配烤肉的绝佳葡萄酒。长而光滑的余味完美地衬托了烤架炭赋予食物的烟熏味。\",\r\n \"ProductCode\": \"1306258\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\ "\r\n }\r\n ],\r\n \"WineImageUrl\": \" http://www.clubw.com/Images/Content/product/1306258/001.jpg\"\r\n },\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"PairingId\" : 1003,\r\n \"Information\": \"李子和黑莓的味道使它成为搭配咸肉的绝佳葡萄酒。\",\r\n \"WineId\": 247,\r\ n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n }\r\n ],\r\n \"UsedIngredients\": [\r\n {\r\ n \"$id\": \"4\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\r\ n \"UsedIngredientId\": 1030,\r\n \"Amount\": \"1 Tbs\",\r\n \"IngredientName\": \"橄榄油\",\r\n \"RecipeId \": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \"5\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\ r\n },\r\n \"UsedIngredientId\": 1031,\r\n \"Amount\": \"1 pound\",\r\n \"IngredientName\": \"温和或热意大利香肠\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \ "6\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1032, \r\n \"Amount\": \"1\",\r\n \"IngredientName\": \"洋葱,切碎\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \"7\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1033,\ r\n \"Amount\": \"2\",\r\n \"IngredientName\": \"红甜椒,切成1英寸的小块\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \"8\",\r\n \"食谱\" : {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1034,\r\n \"Amount\": \"1 ¾ tsp\",\r\n \"IngredientName\": \"salt\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\ r\n {\r\n \"$id\": \"9\",\r\n \"食谱\":{\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1035,\r\n \"Amount\": \"3\" ,\r\n \"IngredientName\": \"蒜瓣,剁碎\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\ r\n {\r\n \"$id\": \"10\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r \n },\r\n \"UsedIngredientId\": 1036,\r\n \"Amount\": \"1 cup\",\r\n \"IngredientName\": \"罐装碎番茄puree\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \ "11\",\r\n \"食谱\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1037,\r\n \"数量\": \"1杯\",\r\n \ "IngredientName\": \"罐装低钠鸡汤或自制高汤\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r \n {\r\n \"$id\": \"12\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\ n },\r\n \"UsedIngredientId\": 1038,\r\n \"Amount\": \"2 Tbs\",\r\n \"IngredientName\": \"干苦艾酒或干白葡萄酒\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \" 13\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1039,\r\n \"数量\": \"3 Tbs\",\r\n \ "IngredientName\": \"切碎的欧芹菜\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\ r\n \"$id\": \"14\",\r\n \"配方\": {\r\n \"$ref\": \"1\"\r\n },\ r\n \"UsedIngredientId\": 1040,\r\n \"Amount\": \"¾ 磅\",\r\n \"IngredientName\": \"全麦意大利面(最好是新鲜的*)\ ",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n }\r\n ],\r\n \"RecipeImages\": [\r\ n {\r\n \"$id\": \"15\",\r\n \"RecipeImageId\": 1003,\r\n \"Main\": true,\r\n \"ImageUrl\": \"http://vineyardclubw.blob.core.windows.net/recipe/DSC_0155.JPG\",\r\n \"RecipeId\": 1004,\r\n \"删除\": false\r\n }\r\n ],\r\n \"标签\": [\r \n {\r\n \"$id\": \"16\",\r\n \"TagId\": 2,\r\n \"Name\": \"Fast\",\r \n \"Slug\": \"fast\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n },\r\n {\r\n \"$id\": \"17\",\r\n \"TagId\": 5,\r\n \"Name\": \"Dinner\",\r\n \"Slug\": \"dinner\",\r\n \"食谱\": [\r\n {\r\n \" $ref\": \"1\"\r\n }\r\n ]\r\n },\r\n {\r\n \"$id\": \"18\",\r \n \"TagId\": 1071,\r\n \"名称\": \"猪肉\",\r\n \"蛞蝓\": \"猪肉\",\r\n \"食谱\": [\r\n {\r \n \"$ref\": \"1\"\r\n }\r\n ]\r\n },\r\n {\r\n \"$id\": \"19\ ",\r\n \"TagId\": 1072,\r\n \"名称\": \"Pasta\",\r\n \"Slug\": \"pasta\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n }\r\n ]\r \n}"\r\n \"TagId\": 1072,\r\n \"Name\": \"Pasta\",\r\n \"Slug\": \"pasta\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n }\r\n ]\r\n }"\r\n \"TagId\": 1072,\r\n \"Name\": \"Pasta\",\r\n \"Slug\": \"pasta\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n }\r\n ]\r\n }"

我怎样才能把它变成正确的格式?

4

3 回答 3

3

您正在序列化两次。JsonConvert.SerializeObject 正在生成一个字符串,然后 JsonResult 再次序列化该字符串。

于 2013-08-16T11:11:39.377 回答
0

尝试使用Fiddler,您可以查看各种格式的响应,包括 JSON 的树状视图。

于 2013-08-16T10:46:05.943 回答
0

只需转到http://jsonlint.com/将您的 json 粘贴到那里,按 validate 即可获得 json 的格式化视图。奖励:您的 json 得到验证(如果您使用的是 JSON.net,这不应该是无效的,但以防万一)

于 2013-08-16T10:49:38.973 回答