将Json加载到.NET对象并对结果进行过滤和排序

Program.cs

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace JsonFilterSort
{
	internal class Program
	{
		
		private static void Main(string[] args)
		{
			string url = @"http://agl-developer-test.azurewebsites.net/people.json";
			var client = new System.Net.WebClient();

			ParseJsonToObject(client.DownloadString(url));
		}

        /// 
        ///将json解析为对象
        /// 
        /// The json string.
        public static void ParseJsonToObject(string json)
		{
			JArray jsonArray = JArray.Parse(json);

            //将Json加载到.Net对象
            IList<Owner> owners = jsonArray.Select(x => new Owner
			{
				Name = (string)x["name"],
				Gender = (string)x["gender"],
				Age = (int)x["age"],
				Pets = (x["pets"]).Select(p => new Pet { PetName = (string)p["name"], PetType = (string)p["type"] }).ToList()
			}
			).ToList();

			Console.WriteLine("Male");

            //打印 Male owners拥有的所有猫
            //可以转换为单个Linq语句
            foreach (var item in owners.Where(p => p.Gender == "Male"))
			{
				foreach (var pet in item.Pets.Where(p => p.PetType == "Cat").OrderBy(x => x.PetName))
				{
					Console.WriteLine("-" + pet.PetName);
				}
			}

			Console.WriteLine("Female");

            // Print all cats owned by
            //打印  Female owners拥有的所有猫
            foreach (var item in owners.Where(p => p.Gender == "Female"))
			{
				foreach (var pet in item.Pets.Where(p => p.PetType == "Cat").OrderBy(x => x.PetName))
				{
					Console.WriteLine("-" + pet.PetName);
				}
			}
			
			Console.WriteLine("按任意键继续...");
			Console.ReadKey();
		}
	}

	/// 
	/// Owner 类
	/// 
	public class Owner
	{
		public string Name { get; set; }

		public string Gender { get; set; }

		public int Age { get; set; }

		/// 
		public List<Pet> Pets { get; set; }
	}

	/// 
	/// Pet 类
	/// 
	public class Pet
	{
		public string PetName { get; set; }

		public string PetType { get; set; }
	}
}

people.json

[
  {
    "name": "Bob",
    "gender": "Male",
    "age": 23,
    "pets": [
      {
        "name": "Garfield",
        "type": "Cat"
      },
      {
        "name": "Fido",
        "type": "Dog"
      }
    ]
  },
  {
    "name": "Jennifer",
    "gender": "Female",
    "age": 18,
    "pets": [
      {
        "name": "Garfield",
        "type": "Cat"
      }
    ]
  },
  {
    "name": "Steve",
    "gender": "Male",
    "age": 45,
    "pets": null
  },
  {
    "name": "Fred",
    "gender": "Male",
    "age": 40,
    "pets": [
      {
        "name": "Tom",
        "type": "Cat"
      },
      {
        "name": "Max",
        "type": "Cat"
      },
      {
        "name": "Sam",
        "type": "Dog"
      },
      {
        "name": "Jim",
        "type": "Cat"
      }
    ]
  },
  {
    "name": "Samantha",
    "gender": "Female",
    "age": 40,
    "pets": [
      {
        "name": "Tabby",
        "type": "Cat"
      }
    ]
  },
  {
    "name": "Alice",
    "gender": "Female",
    "age": 64,
    "pets": [
      {
        "name": "Simba",
        "type": "Cat"
      },
      {
        "name": "Nemo",
        "type": "Fish"
      }
    ]
  }
]

运行结果如图:

将Json加载到.NET对象并对结果进行过滤和排序_第1张图片

你可能感兴趣的:(将Json加载到.NET对象并对结果进行过滤和排序)