0

I have a wcf service in .net, which i want to return a named JSON object. This is how i want to return the JSON object:

{"file":"\/9j\/4AAQSkZJRgABAQEASABIAAD\/4"}

But this is how it is returned from the service in c#

"\"\/9j\/4AAQSkZJRgABAQEASABIAAD\/4

This is the code i use to return it

[WebInvoke(Method = "GET",
                ResponseFormat = WebMessageFormat.Json,
                UriTemplate = "getFile/{fname}")]
    public string GetFile(string name)
    {
        /*
         * Some code (not important)
         */

        return JsonConvert.SerializeObject(System.Convert.ToBase64String(image));
    }
Zeezer
  • 1,503
  • 2
  • 18
  • 33

2 Answers2

1

Create an object with that string as a property. This should work:

return JsonConvert.SerializeObject(
  new { file = System.Convert.ToBase64String(image) }
);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks!, almost. However it returns this "{\"file\":\"\/9}" and i want to return this {"file":"\/9}. Maybe i have to correct this in php? – Zeezer Sep 14 '12 at 08:42
  • I fixed this problem by returning a stream instead. According to this post: http://stackoverflow.com/questions/3078397/returning-raw-json-string-in-wcf – Zeezer Sep 14 '12 at 08:54
0

You have to create an new object.

var file = JsonConvert.SerializeObject(System.Convert.ToBase64String(image));
return Json(new {file},JsonRequstBehavior.AllowGet);
Rikard
  • 3,828
  • 1
  • 24
  • 39