我有一个看起来像这样的json字符串(简化版):
{"Statistics":[
{"Direction":"2-WAY","AADT":8129,"BC":254},
{"Direction":"NEG","AADT":3956,"BC":124},
{"Direction":"POS","AADT":4173,"BC":128}
]
}
我还启动了一个c#程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace TrafficCounterJSON {
class Program {
static void Main(string[] args) {
string jsonString = "{\"Statistics\":[{\"Direction\":\"2-WAY\",\"AADT\":8129},{\"Direction\":\"NEG\",\"AADT\":3956}]}";
var jObjectt = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
}//main
}
}
我想知道如何使用Newtonsoft.Json.Linq来查询json字符串中的项目,如下所示:
我想要“ Direction = NEG”的所有统计信息行
我似乎无法找出正确的语法来获得这些结果(通过select ... where子句)
因此,根据下面的输入,我终于为json数据构建了一个模型(我使用了该网站:http://json2csharp.com)
下面是json的完整版本:
{ "RequestId":"068f38ed-532c-442c-aae7-748112926b0d",
"Parameters":{"AgencyId":25,"LocalId":"710541"},
"Statistics":[
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"2-WAY","AADT":8129,"BC":254,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":832,"CommercialPeakHourVolume":24},
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"2-WAY","AADT":8129,"BC":254,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":863,"CommercialPeakHourVolume":16},
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"NEG","AADT":3956,"BC":124,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":234,"CommercialPeakHourVolume":9},
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"NEG","AADT":3956,"BC":124,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":525,"CommercialPeakHourVolume":7},
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"POS","AADT":4173,"BC":128,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":598,"CommercialPeakHourVolume":15},
{"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"POS","AADT":4173,"BC":128,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":338,"CommercialPeakHourVolume":9}
],
"AadtHistory":[
{"Year":2017,"Aadt":8202,"Source":"Grown from 2016"},
{"Year":2016,"Aadt":8129,"Source":null},
{"Year":2015,"Aadt":6194,"Source":"Grown from 2014"},
{"Year":2014,"Aadt":6061,"Source":null},
{"Year":2013,"Aadt":5538,"Source":"Grown from 2012"},
{"Year":2012,"Aadt":5617,"Source":"Grown from 2011"},
{"Year":2011,"Aadt":5623,"Source":null},
{"Year":2000,"Aadt":6090,"Source":"Flowmap"},
{"Year":1997,"Aadt":5210,"Source":"Flowmap"},
{"Year":1992,"Aadt":5360,"Source":"Flowmap"},
{"Year":1988,"Aadt":4330,"Source":"Flowmap"}]
}
这是我现在使用的对应模型:
public class Parameters {
public int AgencyId { get; set; }
public string LocalId { get; set; }
}
public class Statistic {
public DateTime LastCollectionDate { get; set; }
public string Direction { get; set; }
public int AADT { get; set; }
public int BC { get; set; }
public string TimePeriod { get; set; }
public string PeakHour { get; set; }
public int PeakHourVolume { get; set; }
public int CommercialPeakHourVolume { get; set; }
}
public class AadtHistory {
public int Year { get; set; }
public int Aadt { get; set; }
public string Source { get; set; }
}
public class RootObject {
public string RequestId { get; set; }
public Parameters Parameters { get; set; }
public List<Statistic> Statistics { get; set; }
public List<AadtHistory> AadtHistory { get; set; }
}
这里是我使用的统计信息数组(来自json字符串)中的行的拉数据的一小段代码
var myObject = JsonConvert.DeserializeObject<RootObject>(jsonResponseString);
foreach (Statistic stat in myObject.Statistics) {
Console.WriteLine("AADT:" + stat.AADT);
}
答案 0 :(得分:0)
添加模型后,您几乎就在那儿。您现在可以使用Linq根据方向进行过滤。
var filteredList = myObject.Statistics.Where(x=>x.Direction == "NEG");