![](/img/trans.png)
[英]WCF Ajax service - How can I pass an Array or JSON to the service? What should the method parameter type be?
[英]ajax pass array to wcf service
有問題,我從表中獲取數據並將其傳遞給WCF Web服務
第一服務
public string SaveDate(string data) {
JavaScriptSerializer json = new JavaScriptSerializer();
List < string[] > mystring = json.Deserialize < List < string[] >> (data);
return "saved";
}
其次獲取數據和ajax調用
$('#btnPaymentOk').click(function () {
var objList = new Array();
$("table#tblGridProductInformation > tbody > tr").each(function () {
if ($(this).find('td.Qty').text() != '') {
objList.push(new Array($(this).find('td.LocalSalesPrice').text(), $(this).find('td.Total').text()));
//objList.push(new Array("a", "b"));
}
});
$.ajax({
type: "POST",
url: "/WebPOSService.svc/SaveDate",
data: "{data:" + JSON.stringify(objList) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
});
通行證看起來像
{data:[["1.00","1.00"],["0.60","0.60"],["0.40","0.40"]]}
json數組
問題不在於從服務端獲取數據
幫幫我
創建一個數據合同:
[DataContract]
public class Data
{
[DataMember]
public List<string[]> data { get; set; }
}
這是我用來測試的代碼,它可以正常工作。
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace ConsoleApplication
{
[DataContract]
public class Data
{
[DataMember]
public List<string[]> data { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
string jsonTxt = "{data:[[\"1.00\",\"1.00\"],[\"0.60\",\"0.60\"],[\"0.40\",\"0.40\"]]}";
var deserialized = JsonConvert.DeserializeObject<Data>(jsonTxt);
//Use this if you don't want to use DataContract, but you will have to reference Newtonsoft.Json.Linq
List<string[]> fullDeserialized = JsonConvert.DeserializeObject<List<string[]>>(JObject.Parse(jsonTxt)["data"].ToString());
}
}
}
另外,我建議您使用Newtonsoft庫對JSON數據進行序列化/反序列化。 如果還沒有的話,請不要忘記添加對System.Runtime.Serializiation的框架引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.