File: data.json:
[
{
"Items": [
{
"Name": "Item 0"
},
{
"Name": "Item 1"
},
{
"Name": "Item 2"
}
]
}
]
Class:
public partial class ItemHandler : Page
{
string itemsJson;
public ItemHandler()
{
InitializeComponent();
itemsJson = JsonHandler.ReadItems();
foreach (var item in collection)
{
// ???
}
}
}
public class Item
{
public string Name { get; set; }
}
I need to get each item from a string list by name, but i don't know how to deserialize one by one, I confused about Json.NET deserialization tutorials.
CodePudding user response:
try this
void Main()
{
var json ="[{\"Items\":[{\"Name\":\"Item 0\"},{\"Name\":\"Item 1\"},{\"Name\":\"Item 2\"}]}]";
var jD=JsonSerializer.Deserialize<Root[]>(json);
foreach (var items in jD)
{
foreach (var item in items.Items)
{
Console.WriteLine(item.Name);
}
}
}
public class Item
{
public string Name { get; set; }
}
public class Root
{
public List<Item> Items { get; set; }
}