Parse JSON String into List

后端 未结 5 2111
借酒劲吻你
借酒劲吻你 2021-02-14 11:55
string json = \"{\\\"People\\\":[{\\\"FirstName\\\":\\\"Hans\\\",\\\"LastName\\\":\\\"Olo\\\"}
                            {\\\"FirstName\\\":\\\"Jimmy\\\",\\\"LastName\         


        
5条回答
  •  忘了有多久
    2021-02-14 12:48

    Since you are using JSON.NET, personally I would go with serialization so that you can have Intellisense support for your object. You'll need a class that represents your JSON structure. You can build this by hand, or you can use something like json2csharp to generate it for you:

    e.g.

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    public class RootObject
    {
        public List People { get; set; }
    }
    

    Then, you can simply call JsonConvert's methods to deserialize the JSON into an object:

    RootObject instance = JsonConvert.Deserialize(json);
    

    Then you have Intellisense:

    var firstName = instance.People[0].FirstName;
    var lastName = instance.People[0].LastName;
    

提交回复
热议问题