这里是一个实体类
[DataContract(Name="UserInfo")]
public class User
{
[DataMember(Name = "ID")]
public string UserID{get;set;}
[DataMember(Name="Name")]
public string UserName { get; set; }
[DataMember(Name="Pwd")]
public string UserPwd { get; set; }
[DataMember(Name="Sex")]
public bool Sex { get; set; }
[DataMember(Name="Age")]
public int Age { get; set; }
}
不会AJAX,帮你顶,留个印。
你可以在MSDN中搜索关于JavaScriptSerializer的信息
也可以参考这篇文章 http://www.cnblogs.com/myssh/archive/2009/07/07/1518758.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Reflection;
namespace TestCmdApp
{
class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
string json = "{ ClassName: 'UserInfo', ClassData: { Name: 'Gray', Age: 24 } }";
UserInfo user = DeserializeJsonObject<UserInfo>(json);
Console.Read();
}
public static T DeserializeJsonObject<T>(string json) where T : new()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> dic = serializer.DeserializeObject(json) as Dictionary<string, object>;
string className = dic["ClassName"] as string;
Dictionary<string, object> classData = dic["ClassData"] as Dictionary<string, object>;
Type type = typeof(T);
object instance = Activator.CreateInstance(type);
foreach (PropertyInfo property in type.GetProperties())
{
if (property.CanWrite)
{
object value = classData[property.Name];
if (value != null)
{
property.SetValue(instance, value, null);
}
}
}
return (T)Convert.ChangeType(instance, typeof(T));
}
}
}
使用 LinqToJOSN