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

本文关键字:反序列化 失败 json 我的 为什么 | 更新日期: 2023-09-27 18:33:21

我反对使用 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 为空。

我还尝试了另一种使用 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");
    }
}

请给我一些提示。我被困在这里已经有一段时间了。谢谢你们!

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

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

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

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

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