我正在尝试读取文件数据,然后将其传递给类进行进一步处理。我可以看到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();
这是类结构(DepartureFlightData是PriceViewModel类的一部分):
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": "Есть"
},
...
}
答案 0 :(得分:1)
试试这个
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
}