将JSON序列化为对象 [英] Serialize JSON to object
问题描述
我有一张json:
Hi,
I have a json:
{"envelope":{"headers":{"test.source":"source system", "test.agency":"test", "test.entity":"Plan", "test.revisionHistory":{"test.createdBy":"admin", "test.createTimeStamp":"2017-08-09T00:00:00", "test.updatedBy":"admin", "test.updatedTimeStamp":"2017-08-09T00:00:00"}, "test.identifiers":{"test.PlanId":"10000001"}}, "triples":[], "instance":{"PlanId":"10000001", "PlanName":"The Health Plan", "PlanType":"Discount Plan", "MailingState":"CA ", "MailingZipCode":"99999-1503", "MailingAddress":"Some Name LLP, 350 South Street Ave., 13th Floor", "MailingCity":"Los Angeles", "MonitoringExaminer":{"examinerName":"abc", "examinerEmail":"abc@test.ca.gov", "examinerPhone":"916-222-2222"}, "planContact":{"ContactName":"xyz", "ContactEmail":"xyz@Plan.com", "eFileContactPhone":"916-111-1111"}, "testLicensingContact":{"licensingContactName":"xyz", "licensingContactEmail":"xyz@test.ca.gov", "licensingContactPhone":"916-111-1111"}, "financialContact":{"financialContactName":"Jack Doe", "financialContactEmail":"xyz@Plan.com", "financialContactPhone":"916-111-1111"}}, "attachments":null}}
更新:格式化版本(使用) JSON Formatter&验证器 [ ^ ]),以便于阅读...
UPDATE: Formatted version (using JSON Formatter & Validator[^]) for easier reading...
{
"envelope":{
"headers":{
"test.source":"source system",
"test.agency":"test",
"test.entity":"Plan",
"test.revisionHistory":{
"test.createdBy":"admin",
"test.createTimeStamp":"2017-08-09T00:00:00",
"test.updatedBy":"admin",
"test.updatedTimeStamp":"2017-08-09T00:00:00"
},
"test.identifiers":{
"test.PlanId":"10000001"
}
},
"triples":[
],
"instance":{
"PlanId":"10000001",
"PlanName":"The Health Plan",
"PlanType":"Discount Plan",
"MailingState":"CA ",
"MailingZipCode":"99999-1503",
"MailingAddress":"Some Name LLP, 350 South Street Ave., 13th Floor",
"MailingCity":"Los Angeles",
"MonitoringExaminer":{
"examinerName":"abc",
"examinerEmail":"abc@test.ca.gov",
"examinerPhone":"916-222-2222"
},
"planContact":{
"ContactName":"xyz",
"ContactEmail":"xyz@Plan.com",
"eFileContactPhone":"916-111-1111"
},
"testLicensingContact":{
"licensingContactName":"xyz",
"licensingContactEmail":"xyz@test.ca.gov",
"licensingContactPhone":"916-111-1111"
},
"financialContact":{
"financialContactName":"Jack Doe",
"financialContactEmail":"xyz@Plan.com",
"financialContactPhone":"916-111-1111"
}
},
"attachments":null
}
}
我拥有什么尝试过:
如何使用反序列化器将JSON转换为对象?
What I have tried:
Hi ,
How to convert JSON to object using deserializer ?
推荐答案
首先要创建一个将JSON转换为的类结构。您可以使用
1. '将JSON粘贴为类'Visual Studio Addin [ ^ ] ;或者
2.像 JSON Utils 这样的webtool [ ^ ]
#2以上是我个人的最爱,因为你有更好的控制权在生成的代码上。将JSON粘贴到该网站工具中会生成以下类结构:
First thing is to create a class structure to convert the JSON to. You can use
1. ‘Paste JSON As Classes’ Visual Studio Addin[^]; or
2. A webtool like JSON Utils[^]
#2 above is my personal favourite as you have greater control over the code generated. Pasting your JSON into that website tool generates the following class structure:
public class TestRevisionHistory
{
[JsonProperty("test.createdBy")]
public string TestCreatedBy { get; set; }
[JsonProperty("test.createTimeStamp")]
public DateTime TestCreateTimeStamp { get; set; }
[JsonProperty("test.updatedBy")]
public string TestUpdatedBy { get; set; }
[JsonProperty("test.updatedTimeStamp")]
public DateTime TestUpdatedTimeStamp { get; set; }
}
public class TestIdentifiers
{
[JsonProperty("test.PlanId")]
public string TestPlanId { get; set; }
}
public class Headers
{
[JsonProperty("test.source")]
public string TestSource { get; set; }
[JsonProperty("test.agency")]
public string TestAgency { get; set; }
[JsonProperty("test.entity")]
public string TestEntity { get; set; }
[JsonProperty("test.revisionHistory")]
public TestRevisionHistory TestRevisionHistory { get; set; }
[JsonProperty("test.identifiers")]
public TestIdentifiers TestIdentifiers { get; set; }
}
public class MonitoringExaminer
{
[JsonProperty("examinerName")]
public string ExaminerName { get; set; }
[JsonProperty("examinerEmail")]
public string ExaminerEmail { get; set; }
[JsonProperty("examinerPhone")]
public string ExaminerPhone { get; set; }
}
public class PlanContact
{
[JsonProperty("ContactName")]
public string ContactName { get; set; }
[JsonProperty("ContactEmail")]
public string ContactEmail { get; set; }
[JsonProperty("eFileContactPhone")]
public string EFileContactPhone { get; set; }
}
public class TestLicensingContact
{
[JsonProperty("licensingContactName")]
public string LicensingContactName { get; set; }
[JsonProperty("licensingContactEmail")]
public string LicensingContactEmail { get; set; }
[JsonProperty("licensingContactPhone")]
public string LicensingContactPhone { get; set; }
}
public class FinancialContact
{
[JsonProperty("financialContactName")]
public string FinancialContactName { get; set; }
[JsonProperty("financialContactEmail")]
public string FinancialContactEmail { get; set; }
[JsonProperty("financialContactPhone")]
public string FinancialContactPhone { get; set; }
}
public class Instance
{
[JsonProperty("PlanId")]
public string PlanId { get; set; }
[JsonProperty("PlanName")]
public string PlanName { get; set; }
[JsonProperty("PlanType")]
public string PlanType { get; set; }
[JsonProperty("MailingState")]
public string MailingState { get; set; }
[JsonProperty("MailingZipCode")]
public string MailingZipCode { get; set; }
[JsonProperty("MailingAddress")]
public string MailingAddress { get; set; }
[JsonProperty("MailingCity")]
public string MailingCity { get; set; }
[JsonProperty("MonitoringExaminer")]
public MonitoringExaminer MonitoringExaminer { get; set; }
[JsonProperty("planContact")]
public PlanContact PlanContact { get; set; }
[JsonProperty("testLicensingContact")]
public TestLicensingContact TestLicensingContact { get; set; }
[JsonProperty("financialContact")]
public FinancialContact FinancialContact { get; set; }
}
public class Envelope
{
[JsonProperty("headers")]
public Headers Headers { get; set; }
[JsonProperty("triples")]
public IList<object> Triples { get; set; }
[JsonProperty("instance")]
public Instance Instance { get; set; }
[JsonProperty("attachments")]
public object Attachments { get; set; }
}
public class Response
{
[JsonProperty("envelope")]
public Envelope Envelope { get; set; }
}
Nest您需要将原始JSON数据映射到类结构。同样,还有一些库:
1.微软自己的功能:如何:序列化和反序列化JSON数据Microsoft Docs [ ^ ];
2. fastJSON [ ^ ];或者
3. Json.NET - Newtonsoft [ ^ ]
#3是我个人的最爱,因为它允许支持用于复杂的自定义序列化。对于这个解决方案,我将使用#3。您可以使用NuGet将其添加到项目中: NuGet Gallery | Json.NET 10.0.3 [ ^ ]这是我的帮助类:
Nest you need to map the raw JSON data to the class structure. Again, there are a few libraries out there:
1. Microsoft's own functions: How to: Serialize and Deserialize JSON Data | Microsoft Docs[^];
2. fastJSON[^]; or
3. Json.NET - Newtonsoft[^]
#3 is my personal favourite as it allows support for complex custom serialization. For this solution, I'll be using #3. You can add it to your project using NuGet: NuGet Gallery | Json.NET 10.0.3[^] and here is my helper class:
public static class JsonConverter
{
public static string FromClass<T>(T data, bool isEmptyToNull = false, JsonSerializerSettings jsonSettings = null)
{
string response = string.Empty;
if (!EqualityComparer<T>.Default.Equals(data, default(T)))
response = JsonConvert.SerializeObject(data, jsonSettings);
return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
}
public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
response = jsonSettings == null
? JsonConvert.DeserializeObject<T>(data)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);
return response;
}
}
然后使用上述类:
Then to use with the above classes:
string rawJson = "{\"envelope\":{\"headers\":{\"test.source\":\"source system\", \"test.agency\":\"test\", \"test.entity\":\"Plan\", \"test.revisionHistory\":{\"test.createdBy\":\"admin\", \"test.createTimeStamp\":\"2017-08-09T00:00:00\", \"test.updatedBy\":\"admin\", \"test.updatedTimeStamp\":\"2017-08-09T00:00:00\"}, \"test.identifiers\":{\"test.PlanId\":\"10000001\"}}, \"triples\":[], \"instance\":{\"PlanId\":\"10000001\", \"PlanName\":\"The Health Plan\", \"PlanType\":\"Discount Plan\", \"MailingState\":\"CA \", \"MailingZipCode\":\"99999-1503\", \"MailingAddress\":\"Some Name LLP, 350 South Street Ave., 13th Floor\", \"MailingCity\":\"Los Angeles\", \"MonitoringExaminer\":{\"examinerName\":\"abc\", \"examinerEmail\":\"abc@test.ca.gov\", \"examinerPhone\":\"916-222-2222\"}, \"planContact\":{\"ContactName\":\"xyz\", \"ContactEmail\":\"xyz@Plan.com\", \"eFileContactPhone\":\"916-111-1111\"}, \"testLicensingContact\":{\"licensingContactName\":\"xyz\", \"licensingContactEmail\":\"xyz@test.ca.gov\", \"licensingContactPhone\":\"916-111-1111\"}, \"financialContact\":{\"financialContactName\":\"Jack Doe\", \"financialContactEmail\":\"xyz@Plan.com\", \"financialContactPhone\":\"916-111-1111\"}}, \"attachments\":null}}";
var result = JsonConverter.ToClass<Response>(rawJson);
这篇关于将JSON序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!