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

标签 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; }
    }

程序永远不会结束,比如说,调用消息框。我可以看到在 Debug模式下内容被更正加载到 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");
    }
}

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

最佳答案

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

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

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

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

关于c# - 为什么我的 C# json 反序列化失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230340/

相关文章:

c# - 当 DataSource 未返回完整结果集时手动设置 GridView 的 PageCount?

c# - 终止前清理?

java - 禁用每个 ObjectMapper 的字段序列化

json - 为什么用 jq 解析返回 null?

java - 没有固定 JSON 正文的 JAX-RS Restful POST

json - 在 Swift 3 中获取 JSON 元素

c# - 如何使用 Qt Quick 显示富文本格式 (rtf)?

c# - 可移植类库中的操作如何超时?

c# - 从 EntityFramework 4 中的多对多关系中删除

javascript - 在 json 字符串中查找值