解析子json元素jarray c#

时间:2017-08-18 22:07:19

标签: c# json

我正在使用json.net jarray并尝试在c#中解析以下内容,但无法弄清楚如何获取子值,即city。此外,我将需要搜索它们,你能帮助,或指向另一个链接,我搜索,但没有看到任何类似的。

[
  {
    "name": "Test One",
    "address": [
      {
        "street": "123",
        "city": "chicago"
      },
      {
        "street": "456",
        "city": "New York"
      }
    ]

  },
  {
   "name": "Test Two",
    "address": [
      {
        "street": "567",
        "city": "Houston"
      },
      {
        "street": "987",
        "city": "Seattle"
      }
    ]
}
]

3 个答案:

答案 0 :(得分:0)

示例解析代码:

string s = "[ { \"name\": \"Test One\", \"address\": [ { \"street\": \"123\", \"city\": \"chicago\" },"
    + " { \"street\": \"456\", \"city\": \"New York\" } ]}, "
    + "{ \"name\": \"Test Two\", \"address\": [ { \"street\": \"567\", \"city\": \"Houston\" },"
    + " { \"street\": \"987\", \"city\": \"Seattle\" } ] } ]";
var array = JArray.Parse(s);
for (int i = 0; i < array.Count; i++)
{
    var obj = array[i];
    var name = obj.Value<string>("name");
    Console.WriteLine($"Name: {name}");
    var addressArray = (JArray)obj["address"];
    for (int j = 0; j < addressArray.Count; j++)
    {
        var addressObj = addressArray[j];
        var street = addressObj.Value<string>("street");
        var city = addressObj.Value<string>("city");
        Console.WriteLine($"Street: {street}  City: {city}");
    }
}

输出:

Name: Test One
Street: 123  City: chicago
Street: 456  City: New York
Name: Test Two
Street: 567  City: Houston
Street: 987  City: Seattle

答案 1 :(得分:0)

只需声明以下类(请参阅http://json2csharp.com/如何生成它们)

public class jaykzoo
{
    public class Address
    {
        public string street { get; set; }
        public string city { get; set; }
    }

    public class RootObject
    {
        public string name { get; set; }
        public List<Address> address { get; set; }
    }
}

并调用 JsonConvert.DeserializeObject

var results = JsonConvert.DeserializeObject<List<jaykzoo.RootObject>>(jsonx);

Tada,神奇地说你的所有数据都准备好了......

答案 2 :(得分:0)

有不同的方法来实现它。最常见和最干净的方法是3号。

var jsonString = @"[ { ""name"": ""Test One"", ""address"": [ { ""street"": ""123"", ""city"": ""chicago"" }, { ""street"": ""456"", ""city"": ""New York"" } ]},
                    { ""name"": ""Test Two"", ""address"": [ { ""street"": ""567"", ""city"": ""Houston"" }, { ""street"": ""987"", ""city"": ""Seattle"" } ] } ]";

var jarray = JArray.Parse(jsonString);

// 1.) accessing via index
Console.WriteLine(jarray[0]["address"][0]["city"]);

// 2.) accessing via JObject & key
foreach (var jobj in jarray.OfType<JObject>())
{
    foreach (var address in jobj["address"])
    {
        Console.WriteLine(address.Value<string>("city"));
    }

}

// 3.) accessing via Parsed Class & search 
var array = JsonConvert.DeserializeObject<List<Person>>(jsonString);
var personLivingInHouston = array.First(p => p.Address.Any(a => a.City == "Houston"));
Console.WriteLine(personLivingInHouston.Name);

// 4.) accessing via dynamic & search
var dynamicArray = JsonConvert.DeserializeObject<List<dynamic>>(jsonString);
var personLivingInChicaco = dynamicArray.Find(p => p.address[0].city == "chicago");
Console.WriteLine(personLivingInChicaco.name.ToString()); //watch the lowercase property here, because dynamic is treated as JObject (expandoObject)

人类:

public class Person
{
    public string Name { get; set; }
    public List<Address> Address = new List<Address>();
}

public class Address
{
    public string City { get; set; }
    public string Street { get; set; }
}