为什么我的C#json反序列化失败了?

时间:2016-02-05 17:44:08

标签: c# json

我正在反对使用stackoverflow.com网站中提到的所有流行历史方法解码json文件。但是,在我的案例中,它们都没有奏效。 这是我正在处理的json文件的一小部分:

[{
    "lat": [
[-19.1415329],
[-18.822279],
[-18.672287]
    ],
    "lon": [
[135.673416],
[135.547226],
[135.566849]
    ]
}]

您可以在此处查看完整的json文件: ftp://ftp.star.nesdis.noaa.gov/pub/smcd/spb/xjin/icvs/json/npp/2016/20160124_CRIS.FSR.json

using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;

string jsonFtp = @"ftp://ftp.star.nesdis.noaa.gov/pub/smcd/spb/xjin/icvs/json/npp/2016/20160124_CRIS.FSR.json";
getJson(jsonFtp);

private void getJson(string url)
{
    var req = (FtpWebRequest)WebRequest.Create(url);
    using (Stream stream = req.GetResponse().GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream);
        string jsonText = reader.ReadToEnd();
        JavaScriptSerializer oJS = new JavaScriptSerializer();
        RootObject oRootObject = new RootObject();
        oRootObject = oJS.Deserialize<RootObject>(jsonText);
        MessageBox.Show("OK");
    }
}

    public class RootObject
    {
        public List<List<double>> lat { get; set; }
        public List<List<double>> lon { get; set; }
        public List<List<int>> BT900 { get; set; }
        public List<List<int>> BT2500 { get; set; }
        public List<List<int>> BT1030 { get; set; }
        public List<List<int>> BT1500 { get; set; }
        public List<List<double>> iRad900 { get; set; }
        public List<List<double>> iRad2500 { get; set; }
        public List<List<int>> OverallQFlw { get; set; }
        public List<List<int>> OverallQFmw { get; set; }
        public List<List<int>> OverallQFsw { get; set; }
        public List<List<int>> FOV { get; set; }
        public List<List<int>> FOR { get; set; }
        public List<List<int>> Scan { get; set; }
        public List<List<int>> QF11 { get; set; }
        public List<List<int>> QF12 { get; set; }
        public List<List<int>> QF13 { get; set; }
        public List<List<int>> QF14 { get; set; }
        public List<List<int>> QF15 { get; set; }
        public List<List<int>> QF16 { get; set; }
        public List<List<int>> QF2lw { get; set; }
        public List<List<int>> QF2mw { get; set; }
        public List<List<int>> QF2sw { get; set; }
        public List<List<int>> QF32lw { get; set; }
        public List<List<int>> QF32mw { get; set; }
        public List<List<int>> QF32sw { get; set; }
        public List<List<int>> QF33lw { get; set; }
        public List<List<int>> QF33mw { get; set; }
        public List<List<int>> QF33sw { get; set; }
        public List<List<int>> QF34lw { get; set; }
        public List<List<int>> QF34mw { get; set; }
        public List<List<int>> QF34sw { get; set; }
        public List<List<int>> QF35lw { get; set; }
        public List<List<int>> QF35mw { get; set; }
        public List<List<int>> QF35sw { get; set; }
        public List<List<int>> QF41lw { get; set; }
        public List<List<int>> QF41mw { get; set; }
        public List<List<int>> QF41sw { get; set; }
        public List<List<int>> QF42lw { get; set; }
        public List<List<int>> QF42mw { get; set; }
        public List<List<int>> QF42sw { get; set; }
        public List<List<int>> QF43lw { get; set; }
        public List<List<int>> QF43mw { get; set; }
        public List<List<int>> QF43sw { get; set; }
        public List<List<int>> QF44lw { get; set; }
        public List<List<int>> QF44mw { get; set; }
        public List<List<int>> QF44sw { get; set; }
        public List<List<int>> QF45lw { get; set; }
        public List<List<int>> QF45mw { get; set; }
        public List<List<int>> QF45sw { get; set; }
        public List<List<int>> DSWSizeLw { get; set; }
        public List<List<int>> DSWSizeMw { get; set; }
        public List<List<int>> DSWSizeSw { get; set; }
        public List<List<int>> ITWSizeLw { get; set; }
        public List<List<int>> ITWSizeMw { get; set; }
        public List<List<int>> ITWSizeSw { get; set; }
        public List<List<int>> PRTTemps { get; set; }
        public List<List<int>> ImpulseNoiselw { get; set; }
        public List<List<int>> ImpulseNoisemw { get; set; }
        public List<List<int>> ImpulseNoisesw { get; set; }
        public List<List<int>> ZPDFClw { get; set; }
        public List<List<int>> ZPDFCmw { get; set; }
        public List<List<int>> ZPDFCsw { get; set; }
        public List<List<int>> SDRFClw { get; set; }
        public List<List<int>> SDRFCmw { get; set; }
        public List<List<int>> SDRFCsw { get; set; }
        public List<List<int>> ZPDMglw { get; set; }
        public List<List<int>> ZPDMgmw { get; set; }
        public List<List<int>> ZPDMgsw { get; set; }
        public List<List<double>> DSSymmetrylw { get; set; }
        public List<List<double>> DSSymmetrymw { get; set; }
        public List<List<double>> DSSymmetrysw { get; set; }
        public List<List<double>> DSSpcStablw { get; set; }
        public List<List<double>> DSSpcStabmw { get; set; }
        public List<List<double>> DSSpcStabsw { get; set; }
        public List<List<double>> ITSpcStablw { get; set; }
        public List<List<double>> ITSpcStabmw { get; set; }
        public List<List<double>> ITSpcStabsw { get; set; }
        public List<List<double>> ITTempStab { get; set; }
        public List<List<double>> ITTempCons { get; set; }
        public List<List<double>> MeaLaserWL { get; set; }
        public List<List<double>> MonLaserWL { get; set; }
        public List<List<double>> ResLaserWL { get; set; }
        public List<string> Maneuver { get; set; }
        public List<string> V { get; set; }
        public List<string> granuleID { get; set; }
    }

程序永远不会到达终点,比如说,调用消息框。我可以看到在调试模式下将内容更正加载到jsonText中。但是oRootObject为null。

我还尝试了另一种使用Json.net的方法,但仍然没有用。

private void getJson(string url)
{
    var req = (FtpWebRequest)WebRequest.Create(url);
    using (Stream stream = req.GetResponse().GetResponseStream())
    {
        StreamReader reader = new StreamReader(stream);
        string jsonText = reader.ReadToEnd();

        var serializer = new DataContractJsonSerializer(typeof(RootObject));
        RootObject library = (RootObject)serializer.ReadObject(stream);
        MessageBox.Show("OK");
    }
}

请给我一些提示。我一直被困在这里等待。 谢谢你们!

1 个答案:

答案 0 :(得分:4)

你得到的Json是一个RootObject的数组,所以你必须改变

oRootObject = oJS.Deserialize<RootObject>(jsonText);

oRootObject = oJS.Deserialize<RootObject[]>(jsonText);

之后在oRootObject [0]中,您将对json进行反序列化