首页 > 代码库 > Newtonsoft 反序列化字符串

Newtonsoft 反序列化字符串

string json=“[{“name”:”zhangsan”,”age”:”12”},{“name”:”zhangsan”,”age”:”12”}]”

方法1:

JArray arr = (JArray)JsonConvert.DeserializeObject(json);

JObject obj = (JObject)arr[1];

Console.WriteLine(obj["name"]);

Console.WriteLine(arr[1]["name"]);

方法2

一个对象

public class Student

{

  • public string name
  • { get; set; }
  • public int age
  • { get; set; }
  • }

    List<Student> students = JsonConvert.DeserializeObject<List<Student>>(json);

    foreach (Student model in students)
    {
                 //dosomething

    }

    Newtonsoft 反序列化字符串