已解决,感谢G大哥和其他朋友。
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
public class JsonDemo
{
static void Main()
{
string json = @"{
""error"" : 0 ,
""flightData"" : {
""CA1503"" : {
""LowPrice"" : 985,
""HighPrice"" : 1350,
""pc"" : 5,
""fp"" : 5,
""priceData"" : {
""135"": {
""sn"" : 135,
""ins"" : 20,
""pp"" : 985,
""bi"" : ""baidu"",
""dis"": 6.5,
""cb"": ""R"",
""ut"" : 5
}
}
}
}
}";
//Console.WriteLine(json);
FlightInformation flight = JsonHelper.Deserialize<FlightInformation>(json);
Console.WriteLine("flight info:" + flight.flightData["CA1503"].priceData["135"].ins.ToString());
Console.Read();
}
}
public class FlightInformation
{
public int error { get; set; }
public Dictionary<string, Flight> flightData { get; set; }
}
public class Flight
{
public decimal LowPrice { get; set; }
public decimal HighPrice { get; set; }
public int pc { get; set; }
public decimal fp { get; set; }
public Dictionary<string, PlaneTicket> priceData { get; set; }
}
public class PlaneTicket
{
public int sn { get; set; }
public decimal ins { get; set; }
public decimal pp { get; set; }
public string bi { get; set; }
public decimal dis { get; set; }
public string cb { get; set; }
public decimal ut { get; set; }
}
public class JsonHelper
{
public static string ToJson<T>(T obj)
{
System.Web.Script.Serialization.JavaScriptSerializer script = new System.Web.Script.Serialization.JavaScriptSerializer();
return script.Serialize(obj);
}
public static T Deserialize<T>(string sJson) where T : class
{
System.Web.Script.Serialization.JavaScriptSerializer script = new System.Web.Script.Serialization.JavaScriptSerializer();
return script.Deserialize<T>(sJson);
}
}
重新定义了序列化类和序列化方式,已测试通过,支持航班,票价的数组形式。
public class FlightInformation
{
public int error { get; set; }
public Dictionary<string, Flight> flightData { get; set; }
}
public class Flight
{
public decimal LowPrice { get; set; }
public decimal HighPrice { get; set; }
public int pc { get; set; }
public decimal fp { get; set; }
public Dictionary<string, PlaneTicket> priceData { get; set; }
}
public class PlaneTicket
{
public int sn { get; set; }
public decimal ins { get; set; }
public decimal pp { get; set; }
public string bi { get; set; }
public decimal dis { get; set; }
public string cb { get; set; }
public decimal ut { get; set; }
}
public class JsonHelper
{
public static string ToJson<T>(T obj)
{
System.Web.Script.Serialization.JavaScriptSerializer script = new System.Web.Script.Serialization.JavaScriptSerializer();
return script.Serialize(obj);
}
public static T Deserialize<T>(string sJson) where T : class
{
System.Web.Script.Serialization.JavaScriptSerializer script = new System.Web.Script.Serialization.JavaScriptSerializer();
return script.Deserialize<T>(sJson);
}
}
string json = @"{
'error' : 0 ,
'flightData' : {
'CA1503' : {
'LowPrice' : 985,
'HighPrice' : 1350,
'pc' : 5,
'fp' : 5,
'priceData' : {
'135': {
'sn' : 135,
'ins' : 20,
'pp' : 985,
'bi' : 'baidu',
'dis': 6.5,
'cb': 'R',
'ut' : 5
}
}
}
}
}";
这个有问题,帮你修改了下
主要还是2个逗号没加:
""error"" : 0, 和""cb"": ""R"",
加上逗号后可以反序列化成功,DataMember可以不用设置,直接从JSON中获取