gpt4 book ai didi

.net - JSON.NET 返回未格式化的字符串

转载 作者:行者123 更新时间:2023-12-04 04:45:27 26 4
gpt4 key购买 nike

我正在尝试为返回的 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\": \"Whole-Wheat Spaghetti with Sausage \u0026 Peppers\",\r\n \"Serving\": 4,\r\n \"Instructions\": \"\u003cp\u003e\u003cspan style=\\"font-size:16px\\"\u003e\u003cspan style=\\"font-family:georgia,serif\\"\u003e1. In a large frying pan, heat the oil over moderate heat. Add the sausage and cook, turning, until browned and cooked through, about 8 minutes. Remove. When the sausage is cool enough to handle, cut it into 1/2-inch slices.\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\\"\u003e2. In a large pot of boiling, salted water, cook the spaghetti until just done, about 12 minutes for dried and 2 minutes for fresh.\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. While the pasta cooked add the onion, peppers, and 3/4 teaspoon of the salt to the pan. Cook, stirring frequently, until the vegetables begin to brown, about 5 minutes. Cover and cook, stirring occasionally until the vegetables are soft, about 3 minutes longer. Add the garlic and cook, stirring, for about 30 seconds. Add the tomatoes, broth, vermouth, the reserved sausage and any accumulated juices, the parsley, and the remaining 1 teaspoon salt and bring to a simmer.\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 \"Pairings\": [\r\n {\r\n \"$id\": \"2\",\r\n \"Wine\": {\r\n \"$id\": \"3\",\r\n \"Pairings\": [\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\": \"http://www.clubw.com/pc/2581789/\",\r\n \"Description\": \"No longer can we stand idly by while people badmouth merlot when there are so many great examples being produced including this wine. The plum and black berry flavors make this an excellent wine to pair with grilled meats. A long smooth finish makes the perfect compliment to the smokiness the grill\u0027s char lends to the food. \",\r\n \"ProductCode\": \"1306258\",\r\n \"Recipes\": [\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 \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"PairingId\": 1003,\r\n \"Information\": \"The plum and black berry flavors make it an excellent wine to pair with salty meats.\",\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\": \"olive oil\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"5\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1031,\r\n \"Amount\": \"1 pound\",\r\n \"IngredientName\": \"mild or hot Italian sausage\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"6\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1032,\r\n \"Amount\": \"1\",\r\n \"IngredientName\": \"onion, chopped\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"7\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1033,\r\n \"Amount\": \"2\",\r\n \"IngredientName\": \"red bell peppers, cut into 1-inch pieces\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"8\",\r\n \"Recipe\": {\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\": \"cloves garlic, minced\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"10\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1036,\r\n \"Amount\": \"1 cup\",\r\n \"IngredientName\": \"canned crushed tomatoes in thick puree\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"11\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1037,\r\n \"Amount\": \"1 cup\",\r\n \"IngredientName\": \"canned low-sodium chicken broth or homemade stock\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": 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\": \"dry vermouth or dry white wine\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"13\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1039,\r\n \"Amount\": \"3 Tbs\",\r\n \"IngredientName\": \"chopped flat-leaf parsley\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": false\r\n },\r\n {\r\n \"$id\": \"14\",\r\n \"Recipe\": {\r\n \"$ref\": \"1\"\r\n },\r\n \"UsedIngredientId\": 1040,\r\n \"Amount\": \"¾ pound\",\r\n \"IngredientName\": \"whole-wheat spaghetti (preferably fresh*)\",\r\n \"RecipeId\": 1004,\r\n \"Delete\": 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 \"Delete\": false\r\n }\r\n ],\r\n \"Tags\": [\r\n {\r\n \"$id\": \"16\",\r\n \"TagId\": 2,\r\n \"Name\": \"Fast\",\r\n \"Slug\": \"fast\",\r\n \"Recipes\": [\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\": \"pork\",\r\n \"Recipes\": [\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 \"Recipes\": [\r\n {\r\n \"$ref\": \"1\"\r\n }\r\n ]\r\n }\r\n ]\r\n}"



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

最佳答案

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

关于.net - JSON.NET 返回未格式化的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18260149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com