gpt4 book ai didi

c# - 从 web api JSON C# 中删除斜线

转载 作者:太空狗 更新时间:2023-10-29 23:47:22 44 4
gpt4 key购买 nike

我有一个 WEB API C#,里面有一个链接数据,例如:images/Chinese/AbaloneEggCustard.jpg

但在 JSON 中,它看起来像这样:

[{"BackgroundImage":"images\/Chinese\/AbaloneEggCustard.jpg", ......}]

我可以知道如何删除斜线吗?需要删除它,希望我可以在与 azure 链接时访问图像。


这是我的 Controller 代码:

public IEnumerable<Food> Get()
{
List<Food> Cases = new List<Food>();
try
{
string connectionString = ConfigurationManager.ConnectionStrings["HealthyFoodDBConnectionString"].ConnectionString;
myConnection = new SqlConnection(connectionString);
myConnection.Open();

string sql = "SELECT * from [Recipe] ";

myCommand = new SqlCommand(sql, myConnection);
myDataReader = myCommand.ExecuteReader();

while (myDataReader.Read())
{
Cases.Add(new Food()
{
RecipeID = (int)myDataReader["RecipeID"],
RecipeTitle = (string)myDataReader["RecipeTitle"],
FoodCategoryID = Convert.ToInt32(myDataReader["FoodCategoryId"]),
Serves = (string)myDataReader["Serves"],
PerServing = (string)myDataReader["PerServing"],
Favourite = ((Convert.ToInt32(myDataReader["Favourite"]) == 1) ? true : false),
Directions = (string)myDataReader["Directions"],
BackgroundImage = (string)myDataReader["BackgroundImage"],
HealthyTips = (string)myDataReader["HealthyTips"],
Nutritions = (string)myDataReader["Nutritions"],
Ingredients = (string)myDataReader["Ingredients"]
});
}
}
finally
{
if (myConnection != null)
myConnection.Close();
}
return Cases;
}

这是我的 Index.cshtml 代码:

<script language="javascript" type="text/javascript">
$(document).ready(function () {
// Send an AJAX request
$.getJSON("api/food/",
function (data) {
// on success, 'data' contains a list of products
$.each(data, function (key, val){

//format the text to display
var str = val.RecipeTitle + ' | ' + val.FoodCategoryID + ' | ' + val.Serves + ' | ' + val.PerServing + ' | ' + val.Favourites + ' | ' + val.Directions + ' | ' + val.BackgroundImage + ' | ' + val.HealthyTips + ' | ' + val.Nutritions + ' | ' + val.Ingredients;

// add a list item for the product
$('<li/>', { html: str }).appendTo($('#cases'));

});
});
});

最佳答案

假设您正在调用 API,并取回正常转义的 JSON 对象:

var myObject = Foo.API.Call(); //returns object with BackgroundImage property.

如果要将结果保存到文本文件,可以使用 JavaScriptSerializer:

var bg = new JavaScriptSerializer().Deserialize(myObject);
using (var writer = new StreamWriter(@"C:\foo.txt"))
{
writer.Write(bg.BackgroundImage);
}

保存的文本文件应该是未转义的字符串。

关于c# - 从 web api JSON C# 中删除斜线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11287631/

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