Reputation: 93
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
Upvotes: 0
Views: 87
Reputation: 1211
As commented on your question, a more convenient way of deserializing your objects, is to use James Newton-King's Json.NET, and is available as a Nuget package.
Sample code:
var vessel = JsonConvert.DeserializeObject<YourType>(json);
This eliminates the need to have the constructor in your domain class. Jon.NET is both faster and highly customizable.
On another note, generally you should not have deserialization in your domain models. In my opinion that would violate the Single Responsibility Principle.
Upvotes: 3
Reputation: 4883
I do not know the JSON that you are having as a response. But based on what you are trying to do, you could do something like this:
JavaScriptSerializer serializer = new JavaScriptSerializer();
Vessel vessel = serializer.DeserializeObject(json);
Also, it does not seem a good idea to deserialize inside the constructor of the class. Just do it on your method.
Upvotes: 2