0

I want to write something like this 1/2

JSONObject requestJson =
                new JSONObject().put("method", "setShutterSpeed")
                        .put("params", new JSONArray().put('1' + "\\" + '/' + '2'))
                        .put("id", id()).put("version", "1.0");
        String url = findActionListUrl(service) + "/" + service;

But everything I tried fails. I always receive something like that:

{"id":14,"method":"setShutterSpeed","version":"1.0","params":["1\\\/2"]}

I try to use the sony cameraremote API with this JSON call

Marie
  • 11
  • 2
  • What language is this? – Alastair McCormack Nov 26 '17 at 21:14
  • Where do you see the result? Printed? Saved to file? Returned to another client? – Alastair McCormack Nov 26 '17 at 21:15
  • Sorry :-) its JAVA – Marie Nov 26 '17 at 21:16
  • I'm guessing you want the result to look like: `{"id":14,"method":"setShutterSpeed","version":"1.0","params":["1/2"]}` ? – Alastair McCormack Nov 26 '17 at 21:16
  • I see the result in the debugger and I transfer this object to a digital camera but the API of the camera says it need to be formated like that 1\/2 – Marie Nov 26 '17 at 21:17
  • and yes it needs to be -> 1 forwardslash backslash 2 – Marie Nov 26 '17 at 21:18
  • `new JSONArray().put("1\\/2")` should be fine – Alastair McCormack Nov 26 '17 at 21:21
  • unfortunately not. I tried this also. Result is {"id":35,"method":"setShutterSpeed","version":"1.0","params":["1\\\/2"]} – Marie Nov 26 '17 at 21:25
  • @AlastairMcCormack `new JSONArray().put("1\\/2")` would be exactly the same string that the OP has tried. (`'1' + "\\" + '/' + '2'` == `"1\\/2"`.) It looks like JSON is doing something special whenever it encounters a backslash. @Daniel You should add JSON or the API you are using to the tags, because this is not just a problem with Java strings; it's related to the JSON API. – Klitos Kyriacou Nov 26 '17 at 21:26
  • Indeed, because JSON requires backslashes to escaped (https://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string), so `"1\\/2"` is sufficient (and yes I'm aware it's equivalent to the OP's string). The receiving client should be decoding accordingly – Alastair McCormack Nov 26 '17 at 21:31
  • @ Klitos Kyriacou thx for your hint. I added the information. I try to use the Sony camera remote API – Marie Nov 26 '17 at 21:32
  • I think the client wants forward slashes to be escaped and your JSON encoder doesn't want to do that. You'll probably have to update the string manually or use a different JSON library. – shmosel Nov 26 '17 at 21:41

0 Answers0