davethecoder
davethecoder

Reputation: 3932

asp.net mvc outputting json with backslashes ( escape) despite many attemps to filter

i have an asp.net controller that output Json as the results

a section of it is here

returnString += string.Format(@"{{""filename"":""{0}"",""line"":[", file.Filename);

what i get returned is this:

"{\"DPI\":\"66.8213457076566\",\"width\":\"563.341067\",\"editable\":\"True\",\"pricecat\":\"6\",\"numpages\":\"2\",\"height\":\"400\",\"page\":[{\"filename\":\"999_9_1.jpg\",\"line\":[]},{\"filename\":\"999_9_2.jpg\",\"line\":[]}]]"

i have tried to return with the following methods:

return Json(returnString);
return Json(returnString.Replace("\\","");

return Json will serialize my string to a jSon string, this i know but it likes to escape for some reason, how can i get rid of it ????

for info this is how i call it with jQuery:

$.ajax({
    url:"/Products/LoadArtworkToJSon",
    type:"POST",
    dataType: "json",
    async: false,
    data:{prodid: prodid },
    success: function(data){ 
        sessvars.myData = data;
        measurements = sessvars.myData;
        $("#loading").remove();

    //empty the canvas and create a new one with correct data, always start on page 0;
    $("#movements").remove();
    $("#canvas").append("<div id=\"movements\" style=\"width:" + measurements.width + "px; height:" 
            + Math.round(measurements.height) 
            + "px; display:block; border:1px solid black; background:url(/Content/products/" 
            + measurements.page[0].filename + ") no-repeat;\"></div>");

your help is much appreciated

thanks

Upvotes: 2

Views: 3918

Answers (1)

Jerod Venema
Jerod Venema

Reputation: 44632

Are you looking at it in the debugger in VS or in the browser? The debugger will include the extra slashes when it displays it, while the actual output will not consist of those values.

Edit: Try passing an object to Json instead of a custom string. Your string is already in Json format (ish), so passing it to Json is re-ecoding it.

return Json(new { filename = "yourfilename" } );

or

return "yourfilename";

...etc, adding in whatever properties you need.

Upvotes: 4

Related Questions