[英]Convert C# classes to Json
How do i serialize multiple c# classes to one json string using .如何使用 .c# 将多个 c# 类序列化为一个 json 字符串? Be able to make the variables in the json data hold values that can change
能够让json数据中的变量保存可以改变的值
string outputJSON = JsonConvert.SerializeObject(); string outputJSON = JsonConvert.SerializeObject();
i have the following classes我有以下课程
public class Rootobject
{
public string Number { get; set; }
public string RequestedDeviceType { get; set; }
public string DeliveryMethod { get; set; }
public Customer Customer { get; set; }
public Vehicle Vehicle { get; set; }
}
public class Customer
{
public Contacts Contacts { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public bool OverrideData { get; set; }
}
public class Contacts
{
public string FirstName { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string City { get; set; }
public string Address { get; set; }
public string MobilePhone { get; set; }
}
public class Vehicle
{
public string VIN { get; set; }
public string MakeModelCode { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int YearOfInitialRegistration { get; set; }
public string MotorType { get; set; }
public bool OverrideData { get; set; }
}
}
They must serialize to the following structure of json structure and i must be able to capture user input and set the values into json file它们必须序列化为以下 json 结构结构,并且我必须能够捕获用户输入并将值设置到 json 文件中
{
"Number": "xTest",
"RequestedDeviceType": "XXXX",
"DeliveryMethod": "XXXX",
"Customer": {
"Contacts": {
"FirstName": "John",
"Name": "Doe",
"Email": "mail@demo.com",
"City": "Harare",
"Address": "XXXXX",
"MobilePhone": "00000000"
},
"Name": "Peter Chaneta",
"Number": "4567865678",
"OverrideData": true
},
"Vehicle": {
"VIN": "weryts55444554",
"MakeModelCode": "34010",
"LicensePlate": "SS 100 GP",
"Make": "RANGE ROVER",
"Model": "SPORT",
"YearOfInitialRegistration": 2016,
"MotorType": "Petrol",
"OverrideData": true
}
}
I am guessing you are using JSON.net. 我猜您正在使用JSON.net。 If not you can get it over the Nuget-Packet Manager.
如果没有,您可以通过Nuget-Packet Manager获取它。
You called your class "Contacts" are you wanting to have more than one contact? 您将班级称为“联系人”吗?您想拥有多个联系人吗? If yes, you might want to use a List on the RootObject.
如果是,则可能要在RootObject上使用列表。
Now if you use: 现在,如果您使用:
var data = new Rootobject();
var dataString = JsonConvert.SerializeObject(data);
You have all your data in a string as requested. 您可以根据要求将所有数据都放在一个字符串中。 From here you could write it to a file.
从这里您可以将其写入文件。 If you want to change the data and read it again you would use something along this:
如果要更改数据并再次读取,则可以使用以下方法:
Rootobject data2 = JsonConvert.DeserializeObject<Rootobject>(dataString);
Now you could display the data somewhere or could change it through any input controls. 现在,您可以在某处显示数据,也可以通过任何输入控件进行更改。 (Website / Windows Form / XAML / Console ...)
(网站/ Windows窗体/ XAML /控制台...)
Any other things you could find at the official documentation for json.net ( https://www.newtonsoft.com/json ) 您可以在json.net的官方文档( https://www.newtonsoft.com/json )上找到任何其他内容
Otherwise your question is way too generic. 否则,您的问题太笼统了。
As an alternative to JSON.Net
you can also use one from System.Web.Extensions
namespace: 作为
JSON.Net
的替代,您还可以使用System.Web.Extensions
命名空间中的一个:
using System.Web.Extensions;
public string SerializeClassToJSON(object baseClass)
{
var jSerializer = new JavascriptSerializer();
var jsonString = jSerializer.Serialize(baseClass);
return jsonString;
}
If you are okay sharing your classes with a third party online, you can use the below link to generate JSON directly from the class.如果您可以在线与第三方分享您的课程,您可以使用以下链接直接从课程中生成 JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.