0

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.

2
  • o.Value<List<JObject>>("results") ? Commented May 9, 2017 at 14:39
  • It might be easier to cast this to a strongly typed object and then using the Newtonsoft Json library to deserialize into the model. Having a strongly typed model also helps you bind to grids a whole lot easier :) Commented May 9, 2017 at 14:41

1 Answer 1

3

you can just use a POO/Models like the following

public class Result
{
    public string apartmentNo { get; set; }
    public string city { get; set; }
    public string floor { get; set; }
    public string houseNo { get; set; }
    public string houseNo2 { get; set; }
    public string phoneNo { get; set; }
    public string postalCode { get; set; }
    public string region { get; set; }
    public string street { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}

public class RootObject
{
    public int resultCount { get; set; }
    public List<Result> results { get; set; }
}

and parse it like this

var root = JsonConvert.DeserializeObject<RootObject>("your json here "); 
Sign up to request clarification or add additional context in comments.

2 Comments

me too ! what he said :P
Works like a charm, I used foreach loop to literate trough list and added result from created class. Thank you very much it realy helped me out, wish you the best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.