2012-09-14 39 views
0

我在.NET中有一个wcf服务,我想要返回一个指定的JSON对象。这是我想要回JSON对象:从.NET中的c#服务返回名为JSON对象

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

但是,这是它是如何从服务返回的C#

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

这是我使用返回它

[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)); 
    } 
代码

回答

1

用该字符串作为属性创建一个对象。这应该工作:

return JsonConvert.SerializeObject(
    new { file = System.Convert.ToBase64String(image) } 
); 
+0

谢谢!差不多。然而它返回这个“{\”file \“:\”\/9}“,我想返回这个{”file“:”\/9}。也许我必须纠正这在PHP? – Zeezer

+0

我通过返回流来解决此问题。根据这篇文章:http://stackoverflow.com/questions/3078397/returning-raw-json-string-in-wcf – Zeezer

0

您必须创建一个新的对象。

var file = JsonConvert.SerializeObject(System.Convert.ToBase64String(image)); 
return Json(new {file},JsonRequstBehavior.AllowGet);