简体   繁体   English

在c#中序列化一个类

[英]serializing a class in c#

I was hoping someone could help me with a problem serialzing data into a class please? 我希望有人可以帮我解决将数据序列化为类的问题吗?

I need to send the following json string to a webservice: 我需要将以下json字符串发送到webservice:

{ "arrivalAt": "2012-12-24T20:00:00.0000000Z", "pickup":{"streetName":"Amaliegade","houseNumber":"36","zipCode":"1256","city":"Copenhagen K","country":"DK","lat":55.68,"lng":12.59}, "dropoff":{"streetName":"Amaliegade","houseNumber":"36","zipCode":"1256","city":"Copenhagen K","country":"DK","lat":55.68,"lng":12.59}, "vehicleType": "fourSeaterAny", "comments": "Hello" }' {“arrivalAt”:“2012-12-24T20:00:00.0000000Z”,“皮卡”:{“streetName”:“Amaliegade”,“houseNumber”:“36”,“zipCode”:“1256”,“city” :“Copenhagen K”,“country”:“DK”,“lat”:55.68,“lng”:12.59},“dropoff”:{“streetName”:“Amaliegade”,“houseNumber”:“36”,“zipCode “:”1256“,”city“:”Copenhagen K“,”country“:”DK“,”lat“:55.68,”lng“:12.59},”vehicleType“:”fourSeaterAny“,”comments“:”你好“ “}'

I put this json string into http://json2csharp.com/ and it generated the following class: 我将这个json字符串放入http://json2csharp.com/ ,它生成了以下类:

public class Pickup
{
public string streetName { get; set; }
public string houseNumber { get; set; }
public string zipCode { get; set; }
public string city { get; set; }
public string country { get; set; }
public double lat { get; set; }
public double lng { get; set; }
}

public class Dropoff
{
public string streetName { get; set; }
public string houseNumber { get; set; }
public string zipCode { get; set; }
public string city { get; set; }
public string country { get; set; }
public double lat { get; set; }
public double lng { get; set; }
}

public class RootObject
{
public string arrivalAt { get; set; }
public Pickup pickup { get; set; }
public Dropoff dropoff { get; set; }
public string vehicleType { get; set; }
public string comments { get; set; }
}

I have managed to do this before but have never had a situation where is there is a class within a class, so to speak. 我之前设法做过这样的事情,但从来没有遇到类中有课的情况,可以这么说。 Meaning the "Pickup" & "DropOff" settings... 意思是“Pickup”和“DropOff”设置......

I am stuck when i try to work out what to do at this line... 当我试图弄清楚在这条线上做什么时,我陷入了困境......

Booking bookingdetails = new ClickATaxi_Classes.Booking(THIS IS WHERE I WILL PUT THE 17 BITS OF INFORMATION BUT HOW?);

I get the feeling there is something i need to do to the class to make it accept arguments but i have no idea where to start and how to send the pickup and dropoff information 我觉得我需要做一些事情才能让它接受参数,但我不知道从哪里开始以及如何发送拾取和下降信息

can anyone help please? 有人可以帮忙吗?

thanks 谢谢

First you should refactor that generated code a little bit 首先,您应该稍微重构生成的代码

public class Location
{
     public string streetName { get; set; }
     public string houseNumber { get; set; }
     public string zipCode { get; set; }
     public string city { get; set; }
     public string country { get; set; }
     public double lat { get; set; }
     public double lng { get; set; }
}

public class Booking
{
     public string arrivalAt { get; set; }
     public Location pickup { get; set; }
     public Location dropoff { get; set; }
     public string vehicleType { get; set; }
     public string comments { get; set; }
}

There is no need for two classes that mean the same thing. 不需要两个意味着相同的类。 After that you will just instantiate the booking object. 之后,您将实例化预订对象。

Booking obj = new Booking { arrivalAt = "ARRIVAL", pickup = new Location { streetName = "", houseNumber = "" ... }, dropoff = new Location { streetName = "", houseNumber = "" ...}, vehicleType = "", comments = "" }

Next you will serialize to a string, I like JSON.NET but you can use any serializer. 接下来,您将序列化为字符串,我喜欢JSON.NET,但您可以使用任何序列化程序。
If you want to use JSON.NET you can install it via Nuget by following these instructions , next add the using statement to the top of your class that will serialize the object: using Newtonsoft.Json; 如果你想使用JSON.NET,可以按照这些说明通过Nuget安装它,然后将using语句添加到将序列化对象的类顶部:using Newtonsoft.Json;

Finally just call JsonConvert 最后只需调用JsonConvert

string json = JsonConvert.SerializeObject(product);

Here are some links to other serializers: 以下是其他序列化程序的一些链接:

Json.NET . Json.NET You need a JSON serializer. 您需要一个JSON序列化程序。 Just pick one that you like. 只需选择一个你喜欢的。 The 3 that have been listed work great. 已经列出的3个很棒。 And make sure you read this article to better understand why you need a JSON serializer. 并确保您阅读本文以更好地理解为什么需要JSON序列化程序。

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

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