將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));
}
/// <summary>
///將json解析為物件
/// </summary>
/// <param name="json">The json string.</param>
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();
}
}
/// <summary>
/// Owner 類
/// </summary>
public class Owner
{
public string Name { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
/// </value>
public List<Pet> Pets { get; set; }
}
/// <summary>
/// Pet 類
/// </summary>
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"
}
]
}
]
執行結果如圖:
相關文章
- ORDER對查詢結果進行排序排序
- .net 對JSON資料排序JSON排序
- SpringBoot整合Jpa對資料進行排序、分頁、條件查詢和過濾Spring Boot排序
- PHP 對資料進行驗證和過濾PHP
- 使用@ResponseBody物件轉json和@RequestBody進行json轉物件案例物件JSON
- Django(67)drf搜尋過濾和排序過濾Django排序
- NC65對單表單據查詢的結果進行排序排序
- .NET CORE 中使用AutoMapper進行物件對映APP物件
- C# JSON按key進行排序C#JSON排序
- 對N個數進行從大到小排序排序
- 對字典進行排序排序
- 陣列filter方法對陣列元素進行過濾陣列Filter
- JS 根據彙總結果過濾JS
- 147. 對連結串列進行插入排序排序
- Django(76)isort工具對import匯入進行排序DjangoImport排序
- Python對字典進行排序Python排序
- 在.net中使用AutoMapper進行物件對映,物件相互轉,簡單方便APP物件
- 大資料MongoDB之mgo驅動如何對查詢結果進行排序(正序逆序多欄位排序)?大資料MongoDB排序
- 將物件解析為JSON資料和將JSON資料解析為物件的簡單例項物件JSON單例
- LeetCode-147-對連結串列進行插入排序LeetCode排序
- mongo對文件中陣列進行過濾的三種方法Go陣列
- 如何在dhtmlxGantt網格中對任務進行排序和重新排序HTML排序
- sqoop進行將Hive 詞頻統計的結果資料傳輸到Mysql中OOPHiveMySql
- JavaScript 根據物件鍵值進行排序JavaScript物件排序
- 「Js」物件按照鍵名來進行排序JS物件排序
- [鐵柱分享]第五期:如何在 Laravel Eloquent 中對 all() 的結果進行排序?Laravel排序
- DRF內建過濾元件與排序元件結合使用元件排序
- 力扣 147. 對連結串列進行插入排序力扣排序
- ELK<logstash>過濾json資料JSON
- 【ASP.NET Core】繫結到 CancellationToken 物件ASP.NET物件
- 給定json資料,將資料與頁面結構進行繫結JSON
- .net core 登入全域性驗證過濾器過濾器
- Jackson JSON包在從物件對映到Json字串過程的迴圈依賴問題-分析與解決JSON物件字串
- python-進階教程-使用物件屬性進行排序Python物件排序
- 學習ASP.NET Core(08)-過濾搜尋與分頁排序ASP.NET排序
- 【Oracle】儲存過程中將動態SQL的多行結果進行迴圈遍歷Oracle儲存過程SQL
- 【ASP.NET Core】MVC過濾器:執行流程ASP.NETMVC過濾器
- http協議獲取實時天氣並對包體進行json解析HTTP協議JSON