我正在尝试读取文件数据,然后将其传递给类进行进一步处理。正如我所看到的,obj
正在接受数据,但随后我出现错误:
Value cannot be null.
Parameter name: source
这是代码:
var url = HttpContext.Current.Server.MapPath(@"~/App_Data/" + username + "/prices.json");
PricesViewModel.DepartureFlightsData dfd = null;
if (File.Exists(url))
{
JObject obj = JObject.Parse(File.ReadAllText(url));
dfd = (from a in obj["PriceItems"]
where a["CityFromID"].Value<string>().Contains(cityfromcode) && a["CityToID"].Value<string>().Contains(citytocode) && a["AirlineCode"].Value<string>().Contains(airlinecode)
select new PricesViewModel.DepartureFlightsData //this line throw exception
{
DepartureCity = a["CityFromName"].Value<string>(),
DepartureAirport = a["AirportFromCode"].Value<string>(),
DepartureDate = depDate,
DepartureTime = a["DepartureTime"].Value<string>(),
DepartureAirline = a["AirlineCode"].Value<string>(),
DepartureFlight = a["DepartureFlight"].Value<string>(),
DepartureFlightId = a["FlightId"].Value<string>(),
Price = a["Price"].Value<int>(),
Currency = a["Currency"].Value<string>()
})
.FirstOrDefault();
这就是类结构(DepartmentFlightData是PricesViewModel类的一部分):
public class DepartureFlightsData
{
public string DepartureCity { get; set; }
public string DepartureAirport { get; set; }
public DateTime DepartureDate { get; set; }
public string DepartureTime { get; set; }
public string DepartureAirline { get; set; }
public string DepartureFlight { get; set; }
public string DepartureFlightId { get; set; }
public int Price { get; set; }
public string Currency { get; set; }
}
好的,JSON在这里:
{
"PriceItems": [
{
"FlightID": "565",
"FlightNumber": "731",
"CityFromID": "1",
"CityFromCode": "MOW",
"CityToID": "19",
"CityToCode": "BCN",
"AirportFromCode": "DME",
"AirportFromName": null,
"AirportToCode": "BCN",
"AirportToName": null,
"DepartureDate": "20150227",
"DepartureTime": "17:00",
"ArrivalDate": "20150227",
"ArrivalTime": "21:00",
"Price": "350",
"Currency": "EU",
"AirServiceID": "89",
"AirCraft": "Boeing",
"AirlineName": "TRANSAERO",
"AirlineCode": "UN",
"PackageID": "232",
"CityFromName": "Москва",
"CityToName": "Барселона",
"TariffName": "Эконом",
"FinalPrice": 700,
"Quota": "Есть"
},
{
"FlightID": "563",
"FlightNumber": "2639",
"CityFromID": "1",
"CityFromCode": "MOW",
"CityToID": "19",
"CityToCode": "BCN",
"AirportFromCode": "SVO",
"AirportFromName": null,
"AirportToCode": "BCN",
"AirportToName": null,
"DepartureDate": "20150227",
"DepartureTime": "11:50",
"ArrivalDate": "20150227",
"ArrivalTime": "19:15",
"Price": "350",
"Currency": "EU",
"AirServiceID": "89",
"AirCraft": "Boeing",
"AirlineName": "Aeroflot-Russian International AirLines",
"AirlineCode": "SU",
"PackageID": "232",
"CityFromName": "Москва",
"CityToName": "Барселона",
"TariffName": "Эконом",
"FinalPrice": 700,
"Quota": "Есть"
},
...
}
试试这个
DepartureFlightsData foo()
{
String jsonText = File.ReadAllText(url);
if(String.IsNullOrEmpty(jsonText)) throw new Exception("JSON data not found");
var flights = Newtonsoft.Json.JsonConvert
.DeserializeObject<IEnumerable<DepartureFlightsData>>(jsonText);
return flights.First(); //will throw exception if it's empty
}