C# 将JSON对象解析为列表

C# 将JSON对象解析为列表,c#,json,windows-phone-7,windows-phone-8,json.net,C#,Json,Windows Phone 7,Windows Phone 8,Json.net,当我尝试使用httpwebrequest而不是webclient解析JSON结果时出现问题(因为webcleint不使用webservice cookies),请帮助我解析它,因为它的结果很复杂 结果是: {"d":"[{\"RefSymbol\":1,\"SymbolFactor\":100000,\"PriceCase\":1,\"RefPriceCase\":1,\"ID\":57,\"Type\":1,\"Name\":\"EUR\/CHF\",\"Bid\":1.2194,\"Ask\

当我尝试使用httpwebrequest而不是webclient解析JSON结果时出现问题(因为webcleint不使用webservice cookies),请帮助我解析它,因为它的结果很复杂

结果是:

{"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(新Uri(“http://xx.xx.xx.xx/mywebservice/)作为HttpWebRequest;
request.Method=“GET”;
request.CookieContainer=cookieJar;
request.BeginGetResponse(ar=>
{
HttpWebRequest req2=(HttpWebRequest)ar.AsyncState;
var响应=(HttpWebResponse)请求2.EndGetResponse(ar);
int numVisibleCookies=response.Cookies.Count;
RootObject root=JsonConvert.DeserializeObject(响应);
},请求);
}

首先,使用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; }
}
公共类结果
{
公共int RefSymbol{get;set;}
公共int符号因子{get;set;}
public int PriceCase{get;set;}
public int RefPriceCase{get;set;}
公共int ID{get;set;}
公共int类型{get;set;}
公共字符串名称{get;set;}
公共双标{get;set;}
公共双询问{get;set;}
公共双高{get;set;}
公共双低位{get;set;}
公共字符串LastQuoteTime{get;set;}
公共整数偏移量{get;set;}
public int PriceOffset{get;set;}
公共字符串类型{get;set;}
公共bool stoptradeifnoprice{get;set;}
公共int StopTradeAfterSeconds{get;set;}
public int MaxAmountPerDeal{get;set;}
公共双端口处理{get;set;}
公共双AskWithSpread{get;set;}
具有扩展{get;set;}
公共整数委员会{get;set;}
public int LimitOffset{get;set;}
公共int StopOffset{get;set;}
公共位置{get;set;}
公共对象扩展{get;set;}
使用公共bool{get;set;}
公共bool IsDisplay{get;set;}
公共布尔HasPriv{get;set;}
公共bool JustClose{get;set;}
公共bool BuyOnly{get;set;}
}
公共类根对象
{
公共列表结果{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);
var request=HttpWebRequest.Create(新Uri(“http://xx.xx.xx.xx/mywebservice/)作为HttpWebRequest;
request.Method=“GET”;
request.CookieContainer=cookieJar;
request.BeginGetResponse(ar=>
{
HttpWebRequest req2=(HttpWebRequest)ar.AsyncState;
使用(var响应=(HttpWebResponse)请求2.EndGetResponse(ar))
{
使用(Stream=response.GetResponseStream())
{
使用(变量读取器=新的流读取器(流))
{
RootObject root=JsonConvert.DeserializeObject(reader.ReadToEnd());
}
}
}
},请求);

请包括一个。不是JSON转储。我尝试过,但没有运气,我用我使用的请求编辑了我的问题(请检查),它给了我错误,请您建议,非常感谢Shawn Kendrot我对您的答案进行了小编辑,它工作正常,您可以检查。嘿,您的编辑被拒绝,因为它没有改进答案。坚持我的答案,然后执行root.result.FirstOrDefault()来获取第一项会更容易。当然,您可以随意命名对象。
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);