在c#中反序列化JSON对象

时间:2017-02-13 14:25:39

标签: c# json serialization

我的问题可能很长,但我相信对于那些熟悉Visual Studio环境的人来说,解决方案非常简单。我很安静,这不是我们的研究领域,所以我们遇到了一些麻烦。我很乐意接受任何帮助!

我遇到了以下问题:

我正在尝试反序列化以下JSON格式:

[
    {
        "id": 1,
        "active": true,
        "air_drop_pos": {
            "latitude": 38.141833,
            "longitude": -76.425263
        },
        "fly_zones": [
            {
                "altitude_msl_max": 200.0,
                "altitude_msl_min": 100.0,
                "boundary_pts": [
                    {
                        "latitude": 38.142544,
                        "longitude": -76.434088,
                        "order": 1
                    },
                    {
                        "latitude": 38.141833,
                        "longitude": -76.425263,
                        "order": 2
                    },
                    {
                        "latitude": 38.144678,
                        "longitude": -76.427995,
                        "order": 3
                    }
                ]
            }
        ],
        "home_pos": {
            "latitude": 38.14792,
            "longitude": -76.427995
        },
        "mission_waypoints": [
            {
                "altitude_msl": 200.0,
                "latitude": 38.142544,
                "longitude": -76.434088,
                "order": 1
            }
        ],
        "off_axis_target_pos": {
            "latitude": 38.142544,
            "longitude": -76.434088
        },
        "emergent_last_known_pos": {
            "latitude": 38.145823,
            "longitude": -76.422396
        },
        "search_grid_points": [
            {
                "altitude_msl": 200.0,
                "latitude": 38.142544,
                "longitude": -76.434088,
                "order": 1
            }
        ]
    }
]

使用命令:  编辑 - >选择性粘贴 - >将JSON粘贴为类 我创建了所需的类来接收所有信息。

然后在C#中我使用以下代码反序列化数据:

using (webResponse3 = (HttpWebResponse)webRequest3.GetResponse()) 
{
     using (var stream = webResponse3.GetResponseStream())
     {
         using (var reader = new StreamReader(stream))
         {
           RootobjectMissions outObject1 = (RootobjectMissions)new JsonSerializer().Deserialize(reader, typeof(RootobjectMissions));
         }
     }
}

我收到以下错误:

The error

将格式更改为正确格式的可能方法是什么?源JSON看起来是一个进入数组Class的数组。可能是什么问题?

我感谢任何帮助!

2 个答案:

答案 0 :(得分:-1)

我不知道RootobjectMissions,但我认为Deserialize需要字符串才能工作而不是流式阅读器

改变这个:

using (var reader = new StreamReader(stream))
     {
       RootobjectMissions outObject1 = (RootobjectMissions)new JsonSerializer().Deserialize(reader, typeof(RootobjectMissions));
     }

对此:

using (var reader = new StreamReader(stream))
     {
       RootobjectMissions outObject1 = (RootobjectMissions)new JsonSerializer().Deserialize(reader.ReadToEnd(), typeof(RootobjectMissions));
     }

答案 1 :(得分:-1)

我使用json2csharp.com来创建示例类。将文件内容反序列化为一个' Mission'实例:

    class Program
    {
        static void Main(string[] args)
        {
            using (var file = File.OpenRead("data.json"))
            {
                using (var reader = new StreamReader(file))
                {
                    var content = reader.ReadToEnd();
                    var missions = JsonConvert.DeserializeObject<Mission[]>(content);
                    System.Console.WriteLine(missions);
                }
            }
        }
    }

    public class AirDropPos
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class BoundaryPt
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int order { get; set; }
    }

    public class FlyZone
    {
        public double altitude_msl_max { get; set; }
        public double altitude_msl_min { get; set; }
        public List<BoundaryPt> boundary_pts { get; set; }
    }

    public class HomePos
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class MissionWaypoint
    {
        public double altitude_msl { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int order { get; set; }
    }

    public class OffAxisTargetPos
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class EmergentLastKnownPos
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class SearchGridPoint
    {
        public double altitude_msl { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int order { get; set; }
    }

    public class Mission
    {
        public int id { get; set; }
        public bool active { get; set; }
        public AirDropPos air_drop_pos { get; set; }
        public List<FlyZone> fly_zones { get; set; }
        public HomePos home_pos { get; set; }
        public List<MissionWaypoint> mission_waypoints { get; set; }
        public OffAxisTargetPos off_axis_target_pos { get; set; }
        public EmergentLastKnownPos emergent_last_known_pos { get; set; }
        public List<SearchGridPoint> search_grid_points { get; set; }
    }