后台代码:
//解析jObject字符串 json_wang 2015/01/27
//var passengerListStr = BookingTicket_PassengerList_HiddenField.Value;
//var TrainFormPassengerList = new List<TrainFormPassenger>();
//if (!string.IsNullOrEmpty(passengerListStr))
//{
// JObject jo = JObject.Parse(passengerListStr);
// foreach (var passengerList in jo.Properties().ToArray())
// {
// var list = passengerList.ToArray();
// for (int i = 0; i < list.Count(); i++)
// {
// var passenger = new TrainFormPassenger();
// passenger.CardType = list[i]["CardType"].ToString();
// passenger.CardNum = list[i]["CardNum"].ToString();
// passenger.PsngrId = list[i]["PsngrId"].ToString();
// TrainFormPassengerList.Add(passenger);
// }
// }
//}
//解析JArray字符串
var jo = JArray.Parse(passengerListStr);
foreach (JObject passengerList in jo.ToArray())
{
var passenger = new TrainFormPassenger();
passenger.CardType = passengerList["CardType"].ToString();
passenger.CardNum = passengerList["CardNum"].ToString();
passenger.PsngrId = passengerList["PsngrId"].ToString();
TrainFormPassengerList.Add(passenger);
}
前台页面:组装json字符串
function passengersSelect() {
var passengerList = new Object(); ////解析JArray字符串则new Array();
$("#tbodyPass tr").each(function (trIndex) {
var passenger = new Object();
var tr = $(this);
//旅客ID
passenger["PsngrId"] = tr[trIndex].id.substring(3);
tr.find("td").each(function (tdIndex, td) {
//证件类型
if (tdIndex == 4) {
passenger["CardType"] = $(td).find("option:selected").val();//选中的值
}
//证件号码
if (tdIndex == 5) {
passenger["CardNum"] = td.innerText;
}
});
passengerList[trIndex] = passenger;
});
//赋值给隐藏域
//先清除
$("#BookingTicket_PassengerList_HiddenField").val("");
$("#BookingTicket_PassengerList_HiddenField").val(JSON.stringify(passengerList));