将JSON对象解析为列表
本文关键字:列表 JSON 对象 | 更新日期: 2023-09-27 17:59:47
当我尝试使用httpwebrequest而不是webclient解析JSON结果时遇到了问题(因为webcleint无法使用webservice cookie),请帮助我解析它,因为它的结果很复杂。
JSON结果为:
{"d":"[{'"RefSymbol'":1,'"SymbolFactor'":100000,'"PriceCase'":1,'"RefPriceCase'":1,'"ID'":57,'"Type'":1,'"Name'":'"EUR'/CHF'",'"Bid'":1.2194,'"Ask'":1.2214,'"High'":1.2215,'"Low'":1.2185,'"LastQuoteTime'":'"24'/04'/2014 18:18:30'",'"SpreadOffset'":0,'"PriceOffset'":0,'"SpreadType'":'"1'",'"StopTradeIfNoPrices'":true,'"StopTradeAfterSeconds'":40,'"MaxAmountPerDeal'":10,'"MinAmountPerDeal'":1,'"AskWithSpread'":1.2214,'"BidWithSpread'":1.2194,'"Commission'":50,'"LimitOffset'":0,'"StopOffset'":0,'"PipLocation'":-4,'"Spread'":20,'"IsUsed'":true,'"IsDisplay'":false,'"HasPriv'":true,'"JustClose'":false,'"BuyOnly'":false},{'"RefSymbol'":1,'"SymbolFactor'":1000,'"PriceCase'":1,'"RefPriceCase'":1,'"ID'":58,'"Type'":1,'"Name'":'"EUR'/JPY'",'"Bid'":141.41,'"Ask'":141.46,'"High'":141.79,'"Low'":141.04,'"LastQuoteTime'":'"24'/04'/2014 18:18:35'",'"SpreadOffset'":0,'"PriceOffset'":0,'"SpreadType'":'"1'",'"StopTradeIfNoPrices'":true,'"StopTradeAfterSeconds'":40,'"MaxAmountPerDeal'":100,'"MinAmountPerDeal'":10,'"AskWithSpread'":141.46,'"BidWithSpread'":141.41,'"Commission'":50,'"LimitOffset'":0,'"StopOffset'":0,'"PipLocation'":-2,'"Spread'":5,'"IsUsed'":true,'"IsDisplay'":false,'"HasPriv'":true,'"JustClose'":false,'"BuyOnly'":false},
....
private void FireRequest2()
{
var request = HttpWebRequest.Create(new Uri("http://xx.xx.xx.xx/mywebservice/")) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookieJar;
request.BeginGetResponse(ar =>
{
HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
var response = (HttpWebResponse)req2.EndGetResponse(ar);
int numVisibleCookies = response.Cookies.Count;
RootObject root = JsonConvert.DeserializeObject<RootObject>(response);
}, request);
}
首先,使用Json2cSharp.com创建类
public class Result
{
public int RefSymbol { get; set; }
public int SymbolFactor { get; set; }
public int PriceCase { get; set; }
public int RefPriceCase { get; set; }
public int ID { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public double High { get; set; }
public double Low { get; set; }
public string LastQuoteTime { get; set; }
public int SpreadOffset { get; set; }
public int PriceOffset { get; set; }
public string SpreadType { get; set; }
public bool StopTradeIfNoPrices { get; set; }
public int StopTradeAfterSeconds { get; set; }
public int MaxAmountPerDeal { get; set; }
public double MinAmountPerDeal { get; set; }
public double AskWithSpread { get; set; }
public double BidWithSpread { get; set; }
public int Commission { get; set; }
public int LimitOffset { get; set; }
public int StopOffset { get; set; }
public int PipLocation { get; set; }
public object Spread { get; set; }
public bool IsUsed { get; set; }
public bool IsDisplay { get; set; }
public bool HasPriv { get; set; }
public bool JustClose { get; set; }
public bool BuyOnly { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
}
然后使用JSON.NET对结果进行反序列化
var request = HttpWebRequest.Create(new Uri("http://xx.xx.xx.xx/mywebservice/")) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookieJar;
request.BeginGetResponse(ar =>
{
HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse) req2.EndGetResponse(ar))
{
using (Stream stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
RootObject root = JsonConvert.DeserializeObject<RootObject>(reader.ReadToEnd());
}
}
}
}, request);