I am trying to find a solution on the below after having read all the similar cases but i am still missing something at the very end.
My JSON classes:
public class Obj
{
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string pollCategory { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Obj> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
The code is:
// After previous statements and functions
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string json = reader.ReadToEnd();
Vessel vessel = new Vessel(json);
Console.WriteLine("imo : " + vessel.imo);
Console.WriteLine("boatName : " + vessel.boatName);
// etc etc
public class Vessel
{
public Vessel(string json)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<dynamic>(json);
imo = (string)jsonObject["vessel"]["imo"];
boatName = (string)jsonObject["vessel"]["boatName"];
vesselType = (string)jsonObject["vessel"]["vesselType"];
callSign = jsonObject["vessel"]["callSign"];
mmsi = (string)jsonObject["vessel"]["mmsi"];
gpsTimeStamp = (string)jsonObject["vessel"]["gpsTimeStamp"];
lat = (string)jsonObject["vessel"]["lat"];
lon = jsonObject["vessel"]["lon"];
cog = (string)jsonObject["vessel"]["cog"];
sog = (string)jsonObject["vessel"]["sog"];
aisr = (string)jsonObject["vessel"]["aisr"];
pollMessage = jsonObject["vessel"]["pollMessage"];
}
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string aisr { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Vessel> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
}
But the Console.WriteLine will not give any results.
The obj after debugging appears to be null.
EDIT:
I needed the following changes:
string json = reader.ReadToEnd();
var vessel = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine("responseCode : " + vessel.responseCode);
Console.WriteLine("imo : " + vessel.obj[0].imo);
and the classes are:
public class Obj
{
public string imo { get; set; }
public string boatName { get; set; }
public string vesselType { get; set; }
public string callSign { get; set; }
public string mmsi { get; set; }
public string gpsTimeStamp { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string cog { get; set; }
public string sog { get; set; }
public string pollCategory { get; set; }
public string pollMessage { get; set; }
}
public class RootObject
{
public List<Obj> obj { get; set; }
public int objCount { get; set; }
public string responseMessage { get; set; }
public int responseCode { get; set; }
public bool dataTruncated { get; set; }
}
Thanks to http://json2csharp.com/ and JSON.NET