当前位置: 代码迷 >> ASP.NET >> 如何在.net后台中获取jquery传递的json
  详细解决方案

如何在.net后台中获取jquery传递的json

热度:4991   发布时间:2013-02-25 00:00:00.0
怎么在.net后台中获取jquery传递的json
var invoiceID= "123"//单个值
var editVal = {1,2,3,4,5};//数组
   
var sendData = { "invoiceID": invoiceID, "editVal": editVal };
jQuery.ajax({
  url: "Application/upDate_invoice_base.aspx",
  data: sendData,
  cache: false,
  type: "post",
  beforeSend: function() {
  $("#divMask").ShowMask("showMessage", "messageContent", "childBody");
  },
  success: function() {
  $("#divMask").closeBg("showMessage");
  }
  });

这个服务端的接受
ID = this.Request.Form["invoiceID"]
数组却接受不到,我不想把数组转换为字符串
知道可以用xml或者json序列化后获取,
但是网上资料过少,希望高人给点实例看看

------解决方案--------------------------------------------------------
$(function(){
var jsonText = "jsonstr={'user':[{'username':'A','password':'123' },{'username':'B','password':'456' }]}";
$.ajax({
type:"POST",
data:jsonText,
url:"search.action",
contentType:"application/json"
});
}); 
string jsonstr=Request.Form["jsonstr"];
SerializeObject(object value, params JsonConverter[] converters),序列化,它有个重载方法SerializeObject(object value)
DeserializeObject(string value, Type type),反序列化,它有个重载方法DeserializeObject(string value)
------解决方案--------------------------------------------------------
下载Newtonsoft.Json.dll

序列反序列类
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization.Json;using Newtonsoft.Json;using System.ServiceModel.Web;namespace UtityClass{    public class ExtJson<T>    {        public static T ReadJosn(string strValue)        {            System.IO.MemoryStream Memory = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strValue));            DataContractJsonSerializer dataJson = new DataContractJsonSerializer(typeof(T));            return (T)dataJson.ReadObject(Memory);        }        public static string WriteJson(T strValue)        {            DataContractJsonSerializer dataJson = new DataContractJsonSerializer(typeof(T));            System.IO.MemoryStream Menory = new System.IO.MemoryStream();            dataJson.WriteObject(Menory, strValue);            byte[] jsonByte = new byte[Menory.Length];            Menory.Position = 0L;//[0] int32--[0L] int64            Menory.Read(jsonByte, 0, (int)Menory.Length);            return System.Text.Encoding.UTF8.GetString(jsonByte);        }    }}
  相关解决方案