我需要解析这个JSON并获取seatjson部分并将其传递给另一个函数。
我不知道它是否由于JSON I的结构;我没有将seatjson作为一个整体传递给另一个函数。我在seatjson中得到每个属性。
dynamic dynJson = JsonConvert.DeserializeObject(savedseat.ToString());
foreach (var item in dynJson)
{
var s = item.seatjson;
BookSeats(s, PNR);
}
我的JSON如下:
[
{
"flightNumber":"0345",
"seatjson":{
"totalBasePrice":null,
"totalPrice":null,
"totalTax":null,
"currency":null,
"validatingCarrier":null,
"lastTicketingDate":null,
"fareOptions":null,
"recordLocator":null
}
},
{
"flightNumber":"0464",
"seatjson":{
"totalBasePrice":null,
"totalPrice":null,
"totalTax":null,
"currency":null,
"validatingCarrier":null,
"lastTicketingDate":null,
"fareOptions":null,
"recordLocator":null
}
}
]
答案 0 :(得分:1)
使用类型模型。如果有疑问,请让IDE为您创建它(“编辑”,“选择性粘贴”,“将JSON粘贴为类”),但类似于:
var flights = JsonConvert.DeserializeObject<Flight[]>(json);
当然,您对阵列中的每个Flight
所做的事情取决于您!
DTO,基于猜测:
public class Flight
{
public string flightNumber { get; set; }
public Seat seatjson { get; set; }
}
public class Seat
{
public decimal? totalBasePrice { get; set; }
public decimal? totalPrice { get; set; }
public decimal? totalTax { get; set; }
public string currency { get; set; }
public string validatingCarrier { get; set; }
public DateTime? lastTicketingDate { get; set; }
public string fareOptions { get; set; }
public string recordLocator { get; set; }
}
答案 1 :(得分:0)
怎么样,
public class Seatjson
{
public object totalBasePrice { get; set; }
public object totalPrice { get; set; }
public object totalTax { get; set; }
public object currency { get; set; }
public object validatingCarrier { get; set; }
public object lastTicketingDate { get; set; }
public object fareOptions { get; set; }
public object recordLocator { get; set; }
}
public class RootObject
{
public string flightNumber { get; set; }
public Seatjson seatjson { get; set; }
}
你可以做到
var savedSeats = JsonConvert.DeserializeObject<List<RootObject>>(savedseat.ToString());
答案 2 :(得分:0)
这是我在我的项目中用来从同类json中读取数据的内容
public class Flight
{
public string flightNumber { get; set; }
public Dictionary<string, string> seatjson { get; set; }
}
public List<Flight> GetJsonFileContentInList(string jsonFilePath)
{
Stream jsonStream = MemoryStream(File.ReadAllBytetes(jsonFilePath));
StreamReader reader = new StreamReader(jsonStream);
string json = reader.ReadToEnd();
return new JavaScriptSerializer().Deserialize<List<Flight>>(json);
}