前面2种方法是不需要声明一个Json字符串的类型即可把Json字符串转换为Dictionary对象
而第3种方法则是声明一个Json字符串的强类型对象,然后反序列化为该对象的数据。
List<string> list = new List<string>();
string json = JsonConvert.SerializeObject(new { Name = "jeff", Age = 23, Code = "f04536a3-832c-4ebd-95d0-15af82dbb1a2", PhoneNumber = "15902104019" });
IDictionary<string, JToken> data = JsonConvert.DeserializeObject<JObject>(json);
foreach (var i in data)
{
string key = i.Key;
var value = i.Value;
if (value.Type.ToString() == "String")
{
list.Add(value.ToString());
}
} var result = JsonConvert.DeserializeObject<IDictionary<string, object>>(json);
var name = result["Name"];
var age = result["Age"]; foreach (var p in result.Keys)
{
var value = result[p];
} int j = 0;
foreach (var p in result)
{
var key = p.Key;
var value = p.Value;
var type = p.Value.GetType();
if (type == typeof(string))
{
j += 1;
}
} clsTest obj = JsonConvert.DeserializeObject<clsTest>(json);
var name1 = obj.GetType().GetProperty("Name");
foreach (var p in obj.GetType().GetProperties())
{
var type = p.GetType().Name;
var key = p.Name;
var value = p.GetValue(obj);
}