|
板凳
楼主 |
发表于 2014-3-25 10:38:29
|
只看该作者
但是,不能正常json转换:
public class Person
{
private string _username;
private string _userIP;
private string _userPort;
public string UserName
{
set { _username = value; }
get { return _username; }
}
public string UserIp
{
set { _userIP = value; }
get { return _userIP; }
}
public string UserPort{
set { _userPort = value; }
get { return _userPort; }
}
}
List<Person> list = new List<Person>();
Person p = new Person();
p.UserName = "kkk";
p.UserIp = "127.0.0.1";
p.UserPort = "10000";
list.Add(p);
Person h = new Person();
h.UserName = "jhgf";
h.UserIp = "221.12.35.120";
h.UserPort = "888";
list.Add(h);
string str = ConvertJson.ListToJson(list);
List<Person> plist = (List<Person>)JsonToObject(str, typeof(Person));
public static object JsonToObject(string jsonString, object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
return serializer.ReadObject(mStream);
}
} |
|