我想将c#列表转换为JArray
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想将c#列表转换为JArray相关的知识,希望对你有一定的参考价值。
如果我使用了JARRAY.FROMOBJECT,我得到了一个空的骨骼json,有人可以帮我让我知道如何将列表转换为专业术语。谢谢
class EGTenantCreated
internal string id get; set;
internal string subject get; set;
internal EGData data get; set;
internal string eventType get; set;
internal string eventTime get; set;
internal string dataVersion get; set;
internal string metadataVersion get; set;
internal string topic get; set;
class EGData
internal string TenantId get; set;
internal string TenantName get; set;
internal string AdministratorEmail get; set;
internal string ProductId get; set;
internal string PackageInstanceId get; set;
internal string CorrelationId get; set;
public List<EGTenantCreated> UpdateEGTenant()
DateTime date = new DateTime();
string tenantName = "Tenant" + date.Day.ToString() + date.Month.ToString() +
date.Year.ToString() + "_"
+ date.Hour.ToString() + date.Minute.ToString();
string adminEmail = "user" + date.Day.ToString() + date.Month.ToString() +
date.Year.ToString() + "_"
+ date.Hour.ToString() + date.Minute.ToString() + "@testmail.com";
EGData egData = new EGData();
egData.TenantId = Guid.NewGuid().ToString();
egData.TenantName = tenantName;
egData.AdministratorEmail = adminEmail;
egData.ProductId = "Erp";
egData.PackageInstanceId = "522b0dc0-5db9-437d-97cc-cd4c742b1b22";
egData.CorrelationId = Guid.NewGuid().ToString();
EGTenantCreated egTenant = new EGTenantCreated();
egTenant.id = Guid.NewGuid().ToString();
egTenant.subject = "/DeploymentRegistry/TenantCreated";
egTenant.eventType = "TenantCreated";
egTenant.eventTime = "2019-10-04T20:02:54.9411815Z";
egTenant.dataVersion = "1.0.0";
egTenant.metadataVersion = "1";
egTenant.topic = "topictest";
egTenant.data = egData;
List<EGTenantCreated> list = new List<EGTenantCreated>();
list.Add(egTenant);
return list;
var list = UpdateEGTenant();
var jArray = JArray.FromObject(list); //here i get an empty json
这里是我得到错误的地方,当我打印jArray.ToString()时,我得到了一个空的json框架,并且没有将我的列表转换成期望的jarray对象。这是真实的代码。
答案
您看到的问题是因为您的属性是internal
。要解决此问题,可以使它们成为public
和类internal
(如果未指定修饰符,则为默认值),或者可以将JsonProperty
属性放在每个属性上:
class EGTenantCreated
[JsonProperty]
internal string id get; set;
[JsonProperty]
internal string subject get; set;
[JsonProperty]
internal EGData data get; set;
[JsonProperty]
internal string eventType get; set;
[JsonProperty]
internal string eventTime get; set;
[JsonProperty]
internal string dataVersion get; set;
[JsonProperty]
internal string metadataVersion get; set;
[JsonProperty]
internal string topic get; set;
class EGData
[JsonProperty]
internal string TenantId get; set;
[JsonProperty]
internal string TenantName get; set;
[JsonProperty]
internal string AdministratorEmail get; set;
[JsonProperty]
internal string ProductId get; set;
[JsonProperty]
internal string PackageInstanceId get; set;
[JsonProperty]
internal string CorrelationId get; set;
以上是关于我想将c#列表转换为JArray的主要内容,如果未能解决你的问题,请参考以下文章