c#java反序列化问题

本文关键字:问题 反序列化 c#java | 更新日期: 2023-09-27 18:25:21

我正试图循环遍历一个json文件,并从yahoo api中检索每条记录的标题。有人能给我一些建议,告诉我该怎么做吗。我的代码如下:

   HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20query%3D%22sushi%22%20and%20location%3D%22san%20francisco%2C%20ca%22&format=json&diagnostics=true&callback=cbfunc");
            HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(rep.GetResponseStream());
            string data = sr.ReadToEnd();
            //Console.WriteLine(data); 
            var jss = new JavaScriptSerializer();
            var dict = jss.Deserialize<Dictionary<string,string>>(data);

c#java反序列化问题

您可以设计几个类来匹配yahoo返回的JSON结构。也不要使用callback=cbfunc参数,否则yahoo将返回JSONP而不是JSON:

using System;
using System.Net;
using System.Web.Script.Serialization;
public class YahooResponse
{
    public Query Query { get; set; }
}
public class Query
{
    public Results Results { get; set; }
}
public class Results
{
    public Result[] Result { get; set; }
}
public class Result
{
    public string Title { get; set; }
}
class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            var json = client.DownloadString("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20query%3D%22sushi%22%20and%20location%3D%22san%20francisco%2C%20ca%22&format=json&diagnostics=true")
            var jss = new JavaScriptSerializer();
            var result = jss.Deserialize<YahooResponse>(json);
            foreach (var item in result.Query.Results.Result)
            {
                Console.WriteLine(item.Title);
            }
        }
    }
}

是否需要将json用于客户端目的?如果没有,我可能建议将结果格式更改为xml,format=xml,并将linq改为xml。然后你可以这样做:

if (tickers.Count() == 0)
            throw new InvalidOperationException("The list of tickers cannot be empty.");
        string url = BuildUrl(tickers);
        WebClient http = new WebClient();            
        string xml = http.DownloadString(url);
        List<Quote> quotes = new List<Quote>();
        XDocument doc = XDocument.Parse(xml);
        foreach (var el in doc.Descendants("quote"))
        {
            if (el.Name == "quote")
            {
                quotes.Add(new Quote()
                {
                    Symbol = el.Element("Symbol").Value,                        
                    TradeDate = el.Element("TradeDate").Value,
                    DaysLow = el.Element("DaysLow").IsEmpty ? null : (decimal?)el.Element("DaysLow"),
                    DaysHigh = el.Element("DaysHigh").IsEmpty ? null :(decimal?)el.Element("DaysHigh"),
                    YearLow = el.Element("YearLow").IsEmpty ? null : (decimal?)el.Element("YearLow"),
                    YearHigh = el.Element("YearHigh").IsEmpty ? null : (decimal?)el.Element("YearHigh"),
                    DividendShare = el.Element("DividendShare").IsEmpty ? null : (decimal?)el.Element("DividendShare"),
                    Open = el.Element("Open").IsEmpty ? null : (decimal?)el.Element("Open"),
                    PreviousClose = el.Element("PreviousClose").IsEmpty ? null : (decimal?)el.Element("PreviousClose"),
                    ShortRatio = el.Element("ShortRatio").IsEmpty ? null : (decimal?)el.Element("ShortRatio"),
                    OneyrTargetPrice = el.Element("OneyrTargetPrice").IsEmpty ? null : (decimal?)el.Element("OneyrTargetPrice"),
                    DividendYield = el.Element("DividendYield").IsEmpty ? null : (decimal?)el.Element("DividendYield"),
                    Ask = el.Element("Ask").IsEmpty ?  null : (decimal?)el.Element("Ask"),
                    Bid = el.Element("Bid").IsEmpty ? null : (decimal?)el.Element("Bid"),
                    AskRealtime = el.Element("AskRealtime").IsEmpty ? null : (decimal?)el.Element("AskRealtime"),
                    BidRealtime = el.Element("BidRealtime").IsEmpty ? null : (decimal?)el.Element("BidRealtime"),
                    BookValue = el.Element("BookValue").IsEmpty ? null : (decimal?)el.Element("BookValue"),                                
                    PercentChangeFromYearLow = el.Element("PercentChangeFromYearLow").Value,
                    LastTradeRealtimeWithTime = el.Element("LastTradeRealtimeWithTime").Value,
                    PercebtChangeFromYearHigh = el.Element("PercebtChangeFromYearHigh").Value,
                    LastTradeWithTime = el.Element("LastTradeWithTime").Value,
                    LastTradePriceOnly = el.Element("LastTradePriceOnly").Value,
                    Name = el.Element("Name").Value,
                    Notes = el.Element("Notes").Value,
                    LastTradeTime = el.Element("LastTradeTime").Value,
                    TickerTrend = el.Element("TickerTrend").Value,
                    StockExchange = el.Element("StockExchange").Value,
                    PercentChange = el.Element("PercentChange").Value,
                    AverageDailyVolume = (Int64?)el.Element("AverageDailyVolume"),
                    Change_PercentChange = el.Element("Change_PercentChange").Value,
                    Change = el.Element("Change").Value,
                    Commission = el.Element("Commission").Value,
                    ChangeRealtime = el.Element("ChangeRealtime").Value,
                    LastTradeDate = el.Element("LastTradeDate").Value
                });
            }
        }
        return quotes;