简体   繁体   中英

C# Parse A Json File Into A Dynamic Object Containing An Array Of Data?

I have copied the following code from an example on csharpcorner.com explaining how to assign JObject.Parse method output to dynamic object and access value as properties of dynamic object.

string jsonData = @"{
'FirstName':'Jignesh',
'LastName':'Trivedi'
}";
dynamic data = JObject.Parse(jsonData);

Console.WriteLine(string.Concat("Hi ", data.FirstName, " " + data.LastName));
Console.ReadLine();

My question is how could I accomplish the same thing (Create multiple dynamic objects) using a json file that was an array of data like the following:

string jsonData = @"{ 'results': [
    'FirstName':'John',
    'LastName':'Smith'
    },
    {
    'FirstName':'Jignesh',
    'LastName':'Trivedi'}
    ]}";

想通了,计算元素,然后使用如下所示的调用创建带有数据的对象。

data.results[0].FirstName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM