When I use json_encode
function in PHP to encode an Object in json format, it will change urls in output to an string with escape characters like http:\/\/example.com\/apps\/images\/image01.jpg
however C# return a url as is i.e. http://example.com/apps/images/image01.jpg
and will not add any escape characters. Since I'm going to implement a web-service just same as my old PHP web service, I want to know how can I encode a url string in C# to be the same as PHP string.
Asked
Active
Viewed 825 times
-1

VSB
- 9,825
- 16
- 72
- 145
-
1Possible duplicate of [Encode object to JSON](http://stackoverflow.com/questions/2287399/encode-object-to-json) – miken32 Feb 18 '17 at 19:45
-
Did you give up or what? – AbraCadaver Feb 19 '17 at 18:33
2 Answers
1
You don't need to do anything. If you write your string i.e. to file (or console) from PHP
and from C#
you will get the same results without changing anything.
If you really want to replace /
by \/
use String.Replace()
method:
string likePhp = strCSharp.Replace("/", @"\/");

Roman
- 11,966
- 10
- 38
- 47
0
You shouldn't need escaped slashes, so tell PHP not to escape them with JSON_UNESCAPED_SLASHES
. Why try and hack the C# JSON to escape the slashes when it's not needed.:
echo json_encode('http://example.com/apps/images/image01.jpg', JSON_UNESCAPED_SLASHES);
Yields:
http://example.com/apps/images/image01.jpg

AbraCadaver
- 78,200
- 7
- 66
- 87
-
@DavidL: Yes I did. The echo is an example obviouslyshowing that you don't need the escapes. Now the PHP and C# JSON are encoded the same. Why escape in PHP and hack in some escapes in C# when you don't need to escape in PHP. – AbraCadaver Feb 18 '17 at 22:41