I have a WEB API C#, inside it I have Data which is a link, for example: images/Chinese/AbaloneEggCustard.jpg
but in JSON, it appear like this:
[{"BackgroundImage":"images\/Chinese\/AbaloneEggCustard.jpg", ......}]
May I know how can I remove the slash? Need it remove so hopefully I can access the images when I link with azure.
Here's my controller codes:
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;
}
here's my Index.cshtml code:
<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'));
});
});
});
Assuming you're calling the API, and getting back the normally escaped JSON object:
var myObject = Foo.API.Call(); //returns object with BackgroundImage property.
If you're saving the result to a text file, you can use JavaScriptSerializer
:
var bg = new JavaScriptSerializer().Deserialize(myObject);
using (var writer = new StreamWriter(@"C:\foo.txt"))
{
writer.Write(bg.BackgroundImage);
}
The text file saved should be the unescaped string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With