4

I'm trying to get a filename from the server to my client and the slashes are driving me insane.

If I have a fully qualified file path that is

\\machine\share\inga.baz

and I want this to be available on a JSON object on the client

1) what format needs to come in to allow JSON.parse to generate

var file = {"path":"\\machine\share\inga.baz"};
var filepath = file.path;
//filepath = "\\machine\share\inga.baz"

with no client-side conversion of the incoming string?

2) what changes need to be done client-side to sanitize the string such that

var filepath = file.path;

actually contains a resolvable resource name?

K. Alan Bates
  • 3,104
  • 5
  • 30
  • 54
  • 2
    I think you need four slashes for each one like: `file = JSON.parse('{"path":"\\\\\\\\machine\\\\share\\\\inga.baz"}');` –  Mar 31 '16 at 21:52
  • I suspect just [escaping the path string](http://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string) is all you need... – Akshat Mahajan Mar 31 '16 at 21:52
  • Your JSON is simply not properly encoded. – Alexander O'Mara Mar 31 '16 at 21:54
  • 1
    ...wait. I need 4 slashes per slash? ...that did it. (don't understand how this is a bad question though. If it's a dup, link) – K. Alan Bates Mar 31 '16 at 21:55
  • each slash escape the next character, but as there is a string representation inside another string representation, you will need to double the doubled slashes –  Mar 31 '16 at 22:18
  • 1
    Yeah...I need to `json = JSON.parse(...); json = JSON.stringify(json, undefined, 2); json = json.replace(/\\\\/g,'\\');` That gets me what I need. Found myself in slash soup but I was trying to solve the problem in the wrong area of processing and didn't have anybody to bounce ideas off of. – K. Alan Bates Mar 31 '16 at 22:19

0 Answers0