有问题,从表中获取数据并将其传递给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数组
问题不是从服务端获取数据
帮帮我
答案 0 :(得分:0)
创建DataContract:
[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的框架参考,请不要忘记。