2016-02-05 154 views
3

我正在解决与stackoverflow.com网站中提到的所有流行的历史方法解码json文件。但是,他们没有一个在我的情况下工作。 这里是JSON文件的一小片,我处理:为什么我的C#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"); 
    } 
} 

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

+0

什么关于'oJS.Deserialize (jsonText)'? json snippt看起来像一个RootObject数组。你也可以使用[json2csharp](http://json2csharp.com/) –

+2

得到提示。错误究竟构成了什么? – David

+0

您能否向我们展示一些示例输出,如果有的话?如果没有输出,你能告诉我们错误吗?我问,因为我们有些人无法在工作中运行你的代码。此外,我甚至无法访问'jsonFtp' /你的帖子中的链接。这里无法访问。 –

回答

4

JSON的,你得到的是RootObject数组,所以你必须改变

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

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

后,在oRootObject [0]你有你自己的JSON反序列化

+0

谢谢。它的工作原理:var oRootObject =(RootObject [])oJS.Deserialize (jsonText) –