views:

179

answers:

1

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

+2  A: 

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.

jvenema
i am looking in firebug results after i get error that measurements.page is undefined
minus4
in firebug on response i dont think that should inc backslashes too should it ???? if i run alert(JSONstring.make(measurements)); i get double // so they must defo be on there somehow ????????
minus4
If they're in the alert showing backslashes, then you're definitely double-escaping the quotes. My guess is that Json(blah) is expecting an object, not a string, so it's escaping the string improperly. Try as edited above.
jvenema
much appreciated it seems i am getting correct json when i use console.debug({"val":data}); it looks peerfect as expected but i think json is jsoning (is this a word) the json results in some way so i then end up with escaped results, i have tried just returning Content(returnString) but this fails, was thinking it would convert the string to unescaped json therfor giving me the perfect string in jquery
minus4
as for the formentioned code alteration, my results are very complicated with lots of ifs, pages and lines per page like returning a book
minus4
have fixed it with JSON.parse on my jquery side where i have sessvars.MyData = data; it is now sessvars.myData = JSON.parse(data);many thanks for your help you set me on the right trail
minus4
No problem; it can be a painful thing figuring out where all the various escapes happen. My preference is to keep everything in strict object format until the last moment, then pass it out. Good luck w/ your project!
jvenema