json使用

<script type="text/javascript">
$(function () {
  selectChange();
})

function selectChange() {
  var typeid = $("#protype").val();
  var product = $("#ProductInfo1_product").val();

  $.ajax({
    type: "post",
    url: "../PageService/ProductDataContentService.aspx?method=getParameter",
    dataType: "json",
    data: { "typeid": typeid,
    "Taskid": $("#taskid").val(),
    "demandid": $("#demandid").val(),
    "product": product
  },
  success: function (data) {
    //alert(data);
    var json = eval(data);
    var jcontent = "";
    var aTitle = Array();
    var aCont = Array();
    var i = 0;
    var n = 0;
    $.each(json, function (index, item) {
    //循环获取数据
      if (n % 12 == 0 && n != 0) { i++; }
        n++;
        aTitle[i] += "<th class='center'>" + item.name + "</th>";
        if (item.value == "nullnull") {
            aCont[i] += "<td class='center'>" + item.value + "</td>";
      } else {
          aCont[i] += "<td class='center'><input type='text' data='" + item.id + "' class='span1' value='" + item.value + "' /></td>";
      }
    });
    for (var j = 0; j < aTitle.length; j++) {
      jcontent += "<thead><tr>" + aTitle[j] + "</tr></thead><tbody><tr>" + aCont[j] + "</tr></tbody>";
    }

    $("#productC").empty().append(jcontent);
  },
  error: function () {
  alert("<%=Resources.global1.fail_load %>");
  }
});
}


function save() {
  var jsonArr = [];
  $("#productC tbody input").each(function (i, n) {
  var jsonObj = {
  id: $(n).attr("data"),
    value: $(n).val()
  }
  jsonArr.push(jsonObj);
});
var strJson = arrayToJson(jsonArr);
//alert(strJson);

  $.ajax({
    type: "POST",
    url: "../PageService/ProductDataContentService.aspx?method=save",
    data: {
      "json": strJson,
      "Taskid": $("#taskid").val(),
      "DemandsId": $("#demandid").val(),
      "ServiceNo": $("#ProductInfo1_serviceno").val(),
      "protype": $("#protype").val(),
      "product": $("#ProductInfo1_product").val()
     },
      dataType: "json",
      success: function (data) {
      alert(data.msg);
    }
  });
}




function arrayToJson(o) {
var r = [];
if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
if (typeof o == "object") {
if (!o.sort) {
for (var i in o) r.push(i + ":" + arrayToJson(o[i]));
if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
r.push("toString:" + o.toString.toString());
}
r = "{" + r.join() + "}";
} else {
for (var i = 0; i < o.length; i++) {
r.push(arrayToJson(o[i]));
}
r = "[" + r.join() + "]";
}
return r;
}
return o.toString();
}
</script>





/// <summary>
/// 获取产品类型参数和收集产品信息 整合
/// </summary>
public void getParameter() 
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
BaseClass.Common common = new BaseClass.Common();
Dictionary<string, string> dProtParam=common.GetProTypeParam(protype);


ProducDataContentInfo pinfo = new ProducDataContentInfo();
IList<ProducDataContentInfo> list=poperate.GetList(1,1,string.Format(" and taskId={0} and DemandsId={1} and ProductType='{2}' and ProductSerialNumber='{3}'",taskId,demandId,protype,productCode),"id desc"," id asc");
IList<productTypeParam> ptplist=new List<productTypeParam>();
if (list.Count > 0)
{
pinfo = list[0];
IList<productTypeParam> pTypeP = Serializer.Deserialize(pinfo.ProductContent, typeof(IList<productTypeParam>)) as IList<productTypeParam>;
foreach (productTypeParam ptp in pTypeP)
{
if (dProtParam.ContainsKey(ptp.id.ToString()))
{
ptp.name = dProtParam[ptp.id.ToString()];
ptplist.Add(ptp);
}
}
}
else {
productTypeParam ptp = null;
foreach (KeyValuePair<string, string> k in dProtParam) 
{
ptp = new productTypeParam();
ptp.id = k.Key;
ptp.name = k.Value;
ptp.value = "nullnull";
ptplist.Add(ptp);
}
}
string jsonData = Serializer.Serialize(ptplist);
System.Web.HttpContext.Current.Response.Write(jsonData);
}

 

posted @ 2014-09-20 12:03  JIN__JIN  阅读(223)  评论(0编辑  收藏  举报