1

I'm wanting to return the address information from my linq response. I'm close but just can't get the last bit. I'm needing all the address information from each entry.

Can someone assist with the last bit please?

  var allCentres = from p in obj["early_child_centres"]
            select p["address"].Children ();

            foreach (var item in allCentres)
            {
                Console.WriteLine(item);
            }

 {

"early_child_centres": [
{

  "centre_name": "Arncliffe Early Childhood Centre",

  "address": {
    "street_name": "12 Firth St",
    "suburb": "Arncliffe",
    "state": "NSW",
    "postcode": "2205",
    "phone": "9599 2896"
  },
  "special_notes": "",

},

]
}

1 Answer 1

2

You can use a entity to deserialize your json :

[DataContract]
public class Center
{
    [DataMember(Name = "center_name")]
    public string Name { get; set; }
    [DataMember(Name = "special_notes")]
    public string SpecialNotes { get; set; }
    [DataMember(Name = "address")]
    public Address Address { get; set; }

    public Center() { this.Address = new Address();}
}

[DataContract]
public class Address
{
    [DataMember(Name = "street_name")]
    public string StreetName { get; set; }
    [DataMember(Name = "suburb")]
    public string Suburb { get; set; }
    [DataMember(Name = "state")]
    public string State { get; set; }
    [DataMember(Name = "postcode")]
    public string Postcode { get; set; }
    [DataMember(Name = "phone")]
    public string Phone { get; set; }
}

And deserialize with :

List<Center> centers = JsonConvert.DeserializeObject<List<Center>>(obj["early_child_centres"].ToString());

And then, your linq request :

from c in center
select c.Address
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Joffrey, thank you for providing such a concise and complete answer for me. It worked like a treat.

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.