I am using Json.net for a project I do in C#, and I got lost looking for how to parse my JSON string to Objects.
so in my program I have a class Person that has 3 properties - string Name, int ID, List condition
every time the user adds a person, the program writes it to a file, it looks like this:
{
"Name": "fsdf",
"ID": 234234,
"Conditions": [
"2",
"4",
"6"
]
}
{
"Name": "David",
"ID": 5555555,
"Conditions": [
"2",
"4",
"6"
]
}
The function I use to add new objects to the file-
public void AddPerson(Person person)
{
string output = JsonConvert.SerializeObject(person, Formatting.Indented);
StreamWriter file = new StreamWriter(@"../Data/Persons.json",true);
file.WriteLine(output);
file.Close();
}
Now I have a ComboBox and I want to fill it with all the names from the JSON string. and also there is a TextBox where the user inputs the ID number, and I want it to auto complete the input with the IDs that are already existing in the JSON file.
I think a good way is to parse each JSON object in my file to a Person object (in Persons array), then using a foreach loop add all the IDs in to a list which I can access later, and add all the names to the ComboBox.
using Newtonsofts' Json.net - how do I parse each object in the JSON string to a separate Person object in my program?
*I've been looking through the docs and I cant find a solution.. also tried google but found nothing that addresses my problem. *I started coding again after a long time off, and I am a bit new to JSON so I am sorry if my question is super noob :D.
[ { /* person 1 */ }, { /* person 2 */ }]
.List<Person>
, add the new person to the list, then serialize it out as aList<Person>
.List<Person>
as the type argument...