【发布时间】:2013-08-15 19:24:11
【问题描述】:
我正在尝试为返回的 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\": \"全麦意大利面with Sausage \u0026 Peppers\",\r\n \"Serving\": 4,\r\n \"Instructions\": \"\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 \"PrepTime\": 10,\r\n \"Slug\": \"whole-wheat-spaghetti-with-sausage---peppers\",\r\n \"Author\": \"Club W Kitchen\",\r\n \"CreatedAt\": \"2013-08-02T21:57:42.283\",\r\n \"CookingTime\": 20, \r\n \"TotalTime\": 30,\r\n \"SlugId\": \"6351107746\",\r\n \"配对\": [\r\n {\r\n \" $id\": \"2\",\r\n \"酒\": {\r\n \"$id\": \"3\",\r\n \"配对\": [ \r\n {\r\n \"$ref\": \"2\"\r\n }\r\n ],\r\n \"GlossaryWineLinks\": [],\r\n \ "PersonWineLinks\": [],\r\n \"WineId\": 247,\r\n \"Name\": \"2010 MRLT\",\r\n \"Url\": \"@ 987654321@\",\r\n \"Description\": \"我们不能再袖手旁观,当人们生产出如此多的好例子时,包括 t他的酒。李子和黑莓的味道使它成为搭配烤肉的绝佳葡萄酒。长而光滑的余味完美地衬托了烤架炭赋予食物的烟熏味。 \",\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 \"Recipe\" : {\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 \"数量\": \"1 磅\",\r\n \"Ingredien tName\": \"温和或热意大利香肠\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": 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 \"数量\": \"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 \"Recipe\": {\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 \"数量\": \"1 杯\",\r\n \"IngredientName\": \ "罐装碎番茄浓汤\",\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 \"Amount\": \"1 cup\",\r\n \"IngredientName\": \"罐装低钠鸡汤或自制高汤\",\r\ n \"RecipeId\": 1004,\r\n \"删除\": false\r\n },\r\n {\r\n \"$id\": \"12\",\r \n \"Recipe\": {\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 \"名称\": \"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 \"Recipes\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n },\r\n {\r\n \"$ id\": \"18\",\r\n \"TagId\": 1071,\r\n \"Name\": \"Pork\",\r\n \"Slug\": \"猪肉\",\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 \"Name\": \"Pasta\ ",\r\n \"Slug\": \"pasta\",\r\n \"食谱\": [\r\n {\r\n \"$ref\": \"1\" \r\n }\r\n ]\r\n }\r\n ]\r\n}"
我怎样才能把它变成正确的格式?
【问题讨论】:
-
如果你在谈论在客户端上进行调试,你可以使用
JSON.parse()将其转换为对象字面量,这当然更容易查看。 -
不需要在客户端进行调试——一般来说,如果有更好的格式返回就好了。
-
我明白了。好吧,当您说“更好的格式”时,您可能希望更具体一些,因为这可能有点主观。
-
是的 - 我的意思是常规的“填充”JSON 格式 - 我希望这是有道理的?
-
您能否在上述问题中发布您所指格式的示例?
标签: .net json asp.net-mvc-4 json.net