API返回一堆\ t和\ n

时间:2016-04-08 13:29:01

标签: c# json deserialization

我在这里做错了什么?我正在尝试使用ncaa数据但我在我的数据中得到了一堆\ t和\ n,因此我无法将其序列化为对象。这是我的功能,您可以逐字地运行它,因为它不需要凭据来获取数据。

public string GetGameInfo(DateTime dt)
{
    string content = string.Empty;
    string url = "http://data.ncaa.com/jsonp/scoreboard/baseball/d1/2016/04/06/scoreboard.html";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.ContentType = "application/json";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    Stream resStream = response.GetResponseStream();

    using (StreamReader sr = new StreamReader(resStream))
    {
        content = sr.ReadToEnd();
    }


    return content;
}


/// <summary>
/// Summary description for Ncaa
/// </summary>
namespace Ncaa
{

    public class callbackWrapper
    {
        public List<scoreboard> scoreboard { get; set; }
    }
    public class scoreboard
    {
        public DateTime day { get; set; }
        public List<games> games { get; set; }
    }

    public class games
    {
        public string id { get; set; }
        public string conference { get; set; }
        public string gameState { get; set; }
        public string startDate { get; set; }
        public string startDateDisplay { get; set; }
        public string startTime { get; set; }
        public string startTimeEpoch { get; set; }
        public string currentPeriod { get; set; }
        public string finalMessage { get; set; }
        public string gameStatus { get; set; }
        public string periodStatus { get; set; }
        public string downToGo { get; set; }
        public string timeclock { get; set; }
        public string network_logo { get; set; }
        public string location { get; set; }
        public string contestName { get; set; }
        public string url { get; set; }
        public string highlightsUrl { get; set; }
        public string liveAudioUrl { get; set; }
        public string gameCenterUrl { get; set; }
        //public ChampInfo champInfo { get; set; }
        //public IList<object> videos { get; set; }
        public home home { get; set; }
        public away away { get; set; }
    }

    public class home
    {
        public string teamRank { get; set; }
        public IList<int> RHEBreakdown { get; set; }
        public string iconURL { get; set; }
        public string name { get; set; }
        public string nameRaw { get; set; }
        public string nameSeo { get; set; }
        public string shortname { get; set; }
        public string color { get; set; }
        //public Social social { get; set; }
        public string description { get; set; }
        public string currentScore { get; set; }
        public IList<string> scoreBreakdown { get; set; }
        public string winner { get; set; }
    }

    public class away
    {
        public string teamRank { get; set; }
        public IList<int> RHEBreakdown { get; set; }
        public string iconURL { get; set; }
        public string name { get; set; }
        public string nameRaw { get; set; }
        public string nameSeo { get; set; }
        public string shortname { get; set; }
        public string color { get; set; }
        //public Social social { get; set; }
        public string description { get; set; }
        public string currentScore { get; set; }
        public IList<string> scoreBreakdown { get; set; }
        public string winner { get; set; }
    }

}

protected void Page_Load(object sender, EventArgs e)
{
    var json = GetGameInfo(DateTime.Now);

    //this one doesn't work
    //JsonConvert.DeserializeObject<Ncaa.callbackWrapper>(json);

    //I tried removing the /ts and ns with  no luck too
    json = json.Replace("\t", string.Empty).Replace("\n", string.Empty);

    JsonConvert.DeserializeObject<Ncaa.callbackWrapper>(json);


}

2 个答案:

答案 0 :(得分:1)

首先,回答为什么响应中有大量\t\n s因为它们在您的文件中。它们分别是制表符和新行字符。

我不确定你是如何解析它的,但是大多数解析器都应该能够解决这个问题。如果您自己编写,请发布该代码。

如果返回的数据始终为格式callback(JSON),则可以删除函数调用,只需使用NewtonsoftJSON解析JSON

编辑:在查看添加的代码后,我发现您没有剥离函数调用。您应该将其删除并重试(例如,尝试删除所有内容,直到{字符的第一次出现,最后一次或您喜欢的任何其他方式)

答案 1 :(得分:1)

好的,所以我确实让它运转了。对json这样做有帮助。

json = json.Replace("callbackWrapper(", string.Empty).Replace(");", string.Empty);

然后,只需将其解析为记分板对象,而不是回调包装器对象。