0

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

5
  • 3
    Why dont you just deserialize to the specific type you need?. Commented Feb 17, 2017 at 12:55
  • You should do some debugging before posting a question. Is the json empty? If not, just cut the reading of it and make it static field so we can reproduce the issue. Commented Feb 17, 2017 at 12:55
  • 1
    Look at json.net, and deserialize json to object. Commented Feb 17, 2017 at 12:56
  • @FINDarkside i have already written it: The obj after debugging appears to be null. Commented Feb 17, 2017 at 13:34
  • I didn't ask if obj is null, I asked if your json is just empty string. Glad you got it working, but next time you should include the json so people don't have to guess what you are trying to deserialize. Commented Feb 17, 2017 at 17:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply. Unfortunately the vessel.imo is still null.
Send a pastebin/gist on how your JSON looks like, without that, it's impossible to determine the problem.
You don't need Json.NET though. Seems like it's the new Jquery, everyone suggesting to use it without addressing the actual problem.
@FINDarkside Jquery is a Javascript library last time I checked. This is c#. And yes, I am addressing his problem as good as I can without the actual JSON provided.
Point was that you can do that with the builtin Json deserializer just as well.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.