首页 新闻 会员 周边

json反序列化的问题。

0
悬赏园豆:100 [已解决问题] 解决于 2010-08-19 14:28

已解决,感谢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);
}
}

 

roboth的主页 roboth | 初学一级 | 园豆:28
提问于:2010-08-18 13:37
< >
分享
最佳答案
0

 重新定义了序列化类和序列化方式,已测试通过,支持航班,票价的数组形式。

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);
}
}

 

收获园豆:100
Launcher | 高人七级 |园豆:45045 | 2010-08-18 15:04
CA1503是变化的。如何设置DataMember呢?
roboth | 园豆:28 (初学一级) | 2010-08-18 15:10
@roboth:我正在想办法,你别急。
Launcher | 园豆:45045 (高人七级) | 2010-08-18 15:48
等待您打答案
roboth | 园豆:28 (初学一级) | 2010-08-18 16:12
@roboth:调用方式:FlightInformation flight = JsonHelper.Deserialize<FlightInformation>(jsonString);
Launcher | 园豆:45045 (高人七级) | 2010-08-19 14:06
感谢G大哥的热心帮助。如果用Dictionary<string,object> 那个DataMember也可以解决了。
roboth | 园豆:28 (初学一级) | 2010-08-19 14:30
其他回答(2)
0
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
}
}
}
}
}
";

 


这个有问题,帮你修改了下

jowo | 园豆:2834 (老鸟四级) | 2010-08-18 15:19
0

主要还是2个逗号没加:

 ""error"" : 0,   和""cb"": ""R"",

加上逗号后可以反序列化成功,DataMember可以不用设置,直接从JSON中获取

kyo-yo | 园豆:5587 (大侠五级) | 2010-08-18 15:19
好的,我试试。试完了出结果。
支持(0) 反对(0) roboth | 园豆:28 (初学一级) | 2010-08-18 15:28
测试了一下,DataMember不做设置,序列化是通过了。 可以设计到的对象为null。没有值 如果DataMember做Name设置就可以。 可以Name是变化的。
支持(0) 反对(0) roboth | 园豆:28 (初学一级) | 2010-08-18 15:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册