JSON到C#对象无法输入数据

时间:2017-02-17 12:51:53

标签: c# json deserialization

我在阅读了所有相似的案例之后试图在下面找到解决方案,但我仍然在最后遗漏了一些东西。

我的JSON课程:

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; }
}

代码是:

// 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; }
        }
    }

但是Console.WriteLine不会给出任何结果。

调试后的obj似乎为空。

编辑:

我需要进行以下更改:

            string json = reader.ReadToEnd();

            var vessel = JsonConvert.DeserializeObject<RootObject>(json);

            Console.WriteLine("responseCode : " + vessel.responseCode);
            Console.WriteLine("imo : " + vessel.obj[0].imo);

,课程是:

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; }
    }

感谢http://json2csharp.com/和JSON.NET

2 个答案:

答案 0 :(得分:3)

对你的问题发表评论,一种更方便的反序列化对象的方法是使用James Newton-King的Json.NET,并且可以作为Nuget包使用。

示例代码:

var vessel = JsonConvert.DeserializeObject<YourType>(json);

这消除了在域类中使用构造函数的需要。 Jon.NET既快又高度可定制。

另一方面,通常您不应该在域模型中进行反序列化。在我看来,这将违反单一责任原则。

答案 1 :(得分:2)

我不知道您作为回复的JSON。但根据你想要做的事情,你可以做这样的事情:

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Vessel vessel = serializer.DeserializeObject(json);

此外,在类的构造函数内反序列化似乎不太好。就这样做吧。