测试代码
Dim JsonTxt As String = "{""action"":""save"",""updatedRecords"":[{""cs1"":""bb"",""cs2"":""aa""},{""cs1"":""dd"",""cs2"":""aa"",""cs3"":""aa""}]}"
Dim obj As JsonData = FromJsonTo(Of JsonData)(JsonTxt)’用到的函数
    Public Function FromJsonTo(Of T)(ByVal jsonString As String) As T
        Dim ser As New DataContractJsonSerializer(GetType(T))
        Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))
        Dim jsonObject As T = DirectCast(ser.ReadObject(ms), T)
        ms.Close()
        Return jsonObject
    End Function‘类
Public Class JsonData
    Private Tmp_action As String
    <DataMember()> _
        Public Property action() As String
        Get
            Return Tmp_action
        End Get
        Set(ByVal value As String)
            Tmp_action = value
        End Set
    End Property'updatedRecords的代码怎么写,请高手说说?
    
End Class
怎么获取updatedRecords中数据呢,并且updatedRecords中的名与值都不固定?

解决方案 »

  1.   

    转换成Dictionary<string, object>,用字典遍历
      

  2.   

        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {            ObjJson json = new ObjJson()
                {
                    Action = "save",
                    updatedRecords = new List<Dictionary<string, string>>() 
                    { 
                       new Dictionary<string,string>()
                       { 
                        {"CS1","bb"},
                        {"CS2","aa"}
                       },
                       new Dictionary<string,string>()
                       { 
                        {"CS1","dd"},
                        {"CS2","aa"},
                        {"CS3","aa"}
                       }
                   }
                };
                string jonsstr = ToJson<ObjJson>(json);
            }
            private string ToJson<T>(T obj)
            {
                DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
                MemoryStream ms = new MemoryStream();
                ds.WriteObject(ms, obj);
                string strJSON = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return strJSON;
            }
        }
        [DataContract]
        public class ObjJson
        {
            [DataMember]
            public string Action { get; set; }
            [DataMember]
            public List<Dictionary<string, string>> updatedRecords
            {
                get;
                set;
            }
        }jonsstr   的值:
    {"Action":"save","updatedRecords":[[{"Key":"CS1","Value":"bb"},{"Key":"CS2","Value":"aa"}],[{"Key":"CS1","Value":"dd"},{"Key":"CS2","Value":"aa"},{"Key":"CS3","Value":"aa"}]]}和你的有点不一样。。悲剧,解析得不一样
      

  3.   

    有个问题很绕,你的名与值都不固定,,说明是可变的,json致少键是固定的,你键都在变,怎么解析。
      

  4.   

    是GT-Grid控件保存数据时,传给服务器的一个json串,不同的数据表,字段当然不一样了
      

  5.   

    那具体想怎么办呢。VS2008有一个自动解析json的组件