1

我想从我的 linq 响应中返回地址信息。我很接近,但无法得到最后一点。我需要每个条目的所有地址信息。

有人可以帮忙做最后一点吗?

  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": "",

},

]
}
4

1 回答 1

2

您可以使用实体来反序列化您的 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; }
}

并反序列化:

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

然后,您的 linq 请求:

from c in center
select c.Address
于 2013-03-11T09:21:50.730 回答