简体   繁体   English

在 C# 中反序列化 JSON 对象

[英]Deserializing the JSON object in c#

My problem may be long to read, but I am sure the solution is quite simple for those who are familiar with the Visual Studio environment.我的问题可能读起来很长,但我相信对于熟悉 Visual Studio 环境的人来说,解决方案非常简单。 I am quiet new and this isn't our field of studies, so we have some troubles.我是安静的新人,这不是我们的研究领域,所以我们有一些麻烦。 I will be glad to receive any help!我会很高兴得到任何帮助!

I have encountered the following problem:我遇到了以下问题:

I am trying to Deserialize the following JSON format:我正在尝试反序列化以下 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
            }
        ]
    }
]

Using the command: Edit -> Paste Special -> Paste JSON as class I have created the required class to receive all the information.使用命令: Edit -> Paste Special -> Paste JSON as class我已经创建了接收所有信息所需的类。

Then in the C# I use the following code to deserialize the data:然后在 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));
         }
     }
}

And I get the following error:我收到以下错误:

错误

What is the possible way to change the format to the correct one?将格式更改为正确格式的可能方法是什么? The source JSON looks to be an array which enters an array Class.源 JSON 看起来是一个进入数组类的数组。 What can be the problem?可能是什么问题?

I appreciate any help!我感谢任何帮助!

I do not know RootobjectMissions but i think the Deserialize needs string to work not stream reader so我不知道 RootobjectMissions 但我认为反序列化需要字符串才能工作而不是流阅读器所以

Change this:改变这个:

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

To this:对此:

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

I used json2csharp.com to create the sample classes.我使用 json2csharp.com 创建示例类。 Deserialize the file contents as an array of 'Mission' instances:将文件内容反序列化为“任务”实例数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM