Removing slash from web api JSON C #

I have a WEB API, inside which I have Data, which is a link, for example: images/Chinese/AbaloneEggCustard.jpg

but in JSON it looks like this:

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

Can I learn how to remove a slash? I need to remove it, I hope I can access the images when I contact the azure.


Here are 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 is 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'));

            });
        });
    });
+5
source share
2 answers

Assuming you are calling an API, and returning a normally shielded JSON object:

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

If you save 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 saved text file must be a string without saving.

+1
source

You can use this:

string deserializedString = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(serializedString);
0
source

All Articles