I got following json string from response from server that looks like this:
{
"resultCount": 2,
"results": [
{
"apartmentNo": "",
"city": "BEOGRAD",
"floor": "",
"houseNo": "99",
"houseNo2": "",
"phoneNo": "011\/000-0000",
"postalCode": "11000",
"region": "SOME REGION",
"street": "SOME STREET",
"firstName": "FNAME",
"lastName": "LNAME"
},
{
"apartmentNo": "",
"city": "BEOGRAD",
"floor": "",
"houseNo": "99",
"houseNo2": "",
"phoneNo": "011\/000-0000",
"postalCode": "11000",
"region": "SOME REGION",
"street": "SOME STREET",
"firstName": "FNAME",
"lastName": "LNAME"
}
]
}
As it can be seen, there is two results in json response that belongs to "result" and it is separated like {...first...},{...second...}. I already know how to handle only one result, but how should I handle two or more results like this json example?
I want to add this data to data grid view to show result to user.
My code to parse a single result is:
JObject o = JObject.Parse(responseText);
string ime = o["results"]["firstName"].ToString();
string prezime = o["results"]["lastName"].ToString();
string adresa = o["results"]["street"].ToString() + " " + o["results"]["houseNo"].ToString();
string mesto = o["results"]["city"].ToString();
string pbroj = o["results"]["postalCode"].ToString();
string tel = o["results"]["phoneNo"].ToString();
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(ime, prezime, adresa, mesto, pbroj, tel);
I also found a method around here to literate trought all childs using jtokens but there must be a better way of parsing these in etc. multiple arrays, I am using Newtonsoft.Json.
Thank you very much.
o.Value<List<JObject>>("results")?