这是我的网络服务IRestService.cs
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
}
}
这是我的JSON助手类
public class JSONHelper
{
public static string ToJSON(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJSON(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
我的服务,我正在解析JSOn格式的字符串
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData(string id)
{
id = @"{""contacts"": [{""country"":""Pakistan"",""sunrise"":1381107633,""sunset"":1381149604}]}";
return id;
}
#endregion
}
}
这是我使用的链接并获取错误的JSON
http://116.58.61.180/Website/RestServiceImpl.svc/json/22
和JSON是
{"JSONDataResult":"{\"contacts\": [{\"country\":\"Pakistan\",\"sunrise\":1381107633,\"sunset\":1381149604}]}"}
我做错了什么? 为什么我在这个JSON中得到反斜杠
答案 0 :(得分:0)
您的退货类型是"字符串"并且您将返回一个包含双引号的JSON字符串。为了逃避它,WCF添加了反斜杠。
要摆脱它,你应该在WCF中实现一个返回类型的System.IO.Stream。它只是字符串的序列化形式。
答案 1 :(得分:0)
使用某种json序列化程序,可以将c#对象序列化为json,如t his
答案 2 :(得分:0)
您可以返回object or list of objects
。请查看以下示例代码。有一个GET方法,一个是POST。一个方法将返回RequestData的对象,一个将返回RequestData的List
[ServiceContract]
public interface IDataService
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedResponse)]
List<RequestData> GetUser(Request data);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "UsersList/{id}",RequestFormat=WebMessageFormat.Json)]
RequestData UsersList(string id);
}
public class DataService : IDataService
{
public List<RequestData> GetUser(Request data)
{
List<RequestData> list = new List<RequestData>();
if (data.Name.ToUpper() == "MAIRAJ")
{
list.Add(new RequestData
{
Name = "Mairaj",
Age = 25,
Address = "Test Address"
});
list.Add(new RequestData
{
Name = "Ahmad",
Age = 25,
Address = "Test Address"
});
list.Add(new RequestData
{
Name = "Minhas",
Age = 25,
Address = "Test Address"
});
}
return list;
}
public RequestData UsersList(string userId)
{
if (userId == "1")
{
return new RequestData
{
Name = "Mairaj",
Age = 25,
Address = "Test Address"
};
}
else
{
return new RequestData
{
Name = "Amir",
Age = 25,
Address = "Test Address"
};
}
}
}
[DataContract]
public class RequestData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Address { get; set; }
}
这是web.config
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="WCFWebApp.DataService">
<endpoint address="" binding="webHttpBinding" contract="WCFWebApp.IDataService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>
</configuration>