准备Json字符串以在WCF服务中传递的问题。

时间:2015-01-26 22:06:12

标签: javascript json web-services wcf

我正在尝试在javascript应用程序与WCF服务之间进行通信。我创建的WCF服务提供了以下方法:

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/ExportToXml")]
    void ExportToXml(List<Span> spans, List<Detection> detections);

    [DataContract]
    public class Detection
    {
       [DataMember]
       public int TID { get; set; }

       [DataMember]
       public double Longitude { get; set; }

       [DataMember]
       public double Latitude { get; set; }

       [DataMember]
       public double Height { get; set; }

       [DataMember]
       public int SN { get; set; }

       [DataMember]
       public string TLine_Name { get; set; }
  }

  [DataContract]
  public class Span
  {
       [DataMember]
       public int SN { get; set; }

       [DataMember]
       public double Longitude { get; set; }

       [DataMember]
       public double Latitude { get; set; }

       [DataMember]
       public string TLine_Name { get; set; }
   }

但是,我无法准备json在js客户端传递给这个函数。我准备的json有以下形式:

var input = {
              "spans": [{
                  "SN": 1,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "TLine_Name": "Circuit Test 1"
              }, {
                  "SN": 2,
                  "Longitude": 2000000,
                  "Latitude": 2000000,
                  "TLine_Name": "Circuit Test 2"
              }],

              "detections": [{
                  "TID": 1,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "Height": 15,
                  "SN": 1,
                  "TLine_Name": "Circuit Test 1"
              }, {
                  "TID": 2,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "Height": 12,
                  "SN": 1,
                  "TLine_Name": "Circuit Test 1"
              }, {
                  "TID": 3,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "Height": 14,
                  "SN": 1,
                  "TLine_Name": "Circuit Test 1"
              }, {
                  "TID": 4,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "Height": 10,
                  "SN": 2,
                  "TLine_Name": "Circuit Test 2"
              }, {
                  "TID": 5,
                  "Longitude": 1000000,
                  "Latitude": 1000000,
                  "Height": 8,
                  "SN": 2,
                  "TLine_Name": "Circuit Test 2"
              }]
          };

该服务不喜欢上面的json输入。非常感谢任何有关此事的帮助。

1 个答案:

答案 0 :(得分:0)

我可以将JSON对象发送到在我自己的机器上运行的服务。因此,它不是发送到服务的JSON对象的格式,这是问题所在。你有另一个问题。关于客户的更多细节可能有助于解决它的问题。

我知道您的客户端是一个JavaScript应用程序,但这是我的C#客户端,它成功地将JSON对象发送到服务:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:9001/WCFServices/RestfulService/ExportToXml");
request.ContentType = "text/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string json = @"{ ""spans"": " + 
        @"[{ ""SN"": 1, ""Longitude"": 1000000,""Latitude"": 1000000,""TLine_Name"": ""Circuit Test 1""}, " +
        @"{""SN"": 2, ""Longitude"": 2000000, ""Latitude"": 2000000, ""TLine_Name"": ""Circuit Test 2"" }]," + 
        @"""detections"": " +
        @"[{ ""TID"": 1, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 15,""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " + 
        @"{ ""TID"": 2, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 12, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
        @"{ ""TID"": 3, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 14, ""SN"": 1, ""TLine_Name"": ""Circuit Test 1"" }, " +
        @"{ ""TID"": 4, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 10, ""SN"": 2, ""TLine_Name"": ""Circuit Test 2"" }, " +
        @"{ ""TID"": 5, ""Longitude"": 1000000, ""Latitude"": 1000000, ""Height"": 8, ""SN"": 1, ""TLine_Name"": ""Circuit Test 2"" }] }";
   streamWriter.Write(json);
}
WebResponse ws = request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();
responseStream.Close();