在我的项目中,我必须将动态Json字符串传递给WCF Rest服务。这里的输入/输出都应该是Json格式。我正在使用服务器端代码传递我的数据。这是我的代码
string json = "{\"sjSonData\":[{"
+ "\"log_Id\" : 0,"
+ "\"user_Id\" : 1249,"
+ "\"session_key\" : \"dvnoewcdw\","
+ "\"app_id\" : 1,"
+ "\"app_version\" :\"1.2.7\","
+ "\"app_isOnline\" : true,"
+ "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\","
+ "\"log_typeId\" : 1,"
+ "\"log_description\" : \"valid\""
+ "},"
+ "{"
+ "\"log_Id\" : 1,"
+ "\"user_Id\" : 1249,"
+ "\"session_key\" : \"dvnoewcdw\","
+ "\"app_id\" : 1,"
+ "\"app_version\" : \"1.2.7\","
+ "\"app_isOnline\" : true,"
+ "\"app_dateTimeZone\" : \"1997-07-16T19:20:30+01:00\""
+"}]}";
const string url = "http://localhost/RestServiceImpl.svc/jsoninputdata";
//create an HTTP request to the URL that we need to invoke
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8"; //set the content type to JSON
request.Method = "POST"; //make an HTTP POST
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
//initiate the request
JavaScriptSerializer serializer = new JavaScriptSerializer();
var resToWrite = serializer.Deserialize<Dictionary<string, object>>(json);
streamWriter.Write(restoWrite);
streamWriter.Flush();
streamWriter.Close();
}
// Get the response.
WebResponse response = request.GetResponse();
var streamReader = new StreamReader(response.GetResponseStream());
var result = streamReader.ReadToEnd();
我是WCF服务的新手。我不知道如何将此数据传递给Rest服务。在这里,我的输入数据在每次通话时都有所不同。
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "jsoninputdata")]
string jsoninputdata(List<Dictionary<string, object>> rData);
}
public class RestServiceImpl : IRestServiceImpl
{
public jSonResponseData jsoninputdata(List<Dictionary<string, object>> rData)
{
string json = JsonConvert.SerializeObject(rData);
//Do my stuff here.....
return ojSonRes; // Here i need to return my output mentioned below
}
}
我需要返回如下所示的数据(输出),
条件1:如果两者都保存在db中而没有任何错误,
{"sjSonData":[{
"success":true
}]}
条件2:假设任何一个数据都无法保存在数据库中,那么
{"sjSonData":[
{
"log_Id":1,
"success":false
},
{
"log_Id":2,
"success":true
}
]}
现在我得到了结果,
{"sjSonData":"{}"}
我做错了什么!!!!!还请解释我在web.config文件中要做的所有更改。