Reputation: 65
I am fighting against decoding a json file with all popular historical methods mentioned in the stackoverflow.com site. However, none of them worked in my case. Here is a little piece of the json file that I am dealing with:
[{
"lat": [
[-19.1415329],
[-18.822279],
[-18.672287]
],
"lon": [
[135.673416],
[135.547226],
[135.566849]
]
}]
You can see a complete json file from here: 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; }
}
The program never reaches the end, say, calling the messagebox. I can see that the content is corrected loaded into jsonText under debug mode.But oRootObject is null.
I also tried another method using Json.net but still didn't work.
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");
}
}
Please give me some hints. I have been stuck here for a loooong while. Thank you guys!
Upvotes: 3
Views: 178
Reputation: 7983
The Json that you get is an array of RootObject
so you must change
oRootObject = oJS.Deserialize<RootObject>(jsonText);
to
oRootObject = oJS.Deserialize<RootObject[]>(jsonText);
After that in oRootObject[0] you will have your json deserialized
Upvotes: 4