使用System.Text.Json自定义反序列化-将字段分组到对象中



使用System.Text.Json和。net Core 3.1,我如何为成员反序列化以下JSON:

{
"id": 123,
"firstName": "James",
"lastName": "Smith",
"group": "Premium"
"state": "Active"
}

类:

public class Membership
{
public Member Member { get; set; }
public string Group { get; set; }
public string State { get; set; }
}

public class Member
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

谢谢!

就我个人而言,我会创建一个POCO/Model来匹配Json,就像它被引入并反序列化一样。然后在Membership类中添加一个构造函数,该构造函数接受传入的、反序列化的json模型,并根据需要构建对象。

这是在获取json和返回您自己的模型之间的额外步骤,但是由于源(传入的json)在结构上与目标(您的poco对象)不匹配,因此必须在某处进行转换。我发现当我的代码中有这种明确的分离时,遵循翻译是最容易的。

像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
// I wrapped in [] to make a list
const string inJson = @"[{
""id"": 123,
""firstName"": ""James"",
""lastName"": ""Smith"",
""group"": ""Premium"",
""state"": ""Active""
}]";
public static void Main()
{
var deserialized = JsonSerializer.Deserialize<List<JsonMember>>(inJson);
var asMembership = deserialized.Select(i => new Membership(i)).ToList();

foreach(var m in asMembership){
Console.WriteLine($"Group: {m.Group}");
Console.WriteLine($"State: {m.State}");
Console.WriteLine($"Member Id: {m.Member.Id}");
Console.WriteLine($"Member First Name: {m.Member.FirstName}");
Console.WriteLine($"Member Last Name: {m.Member.LastName}");
}
}
}
public class JsonMember{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("firstName")]
public string FirstName { get; set; }
[JsonPropertyName("lastName")]
public string LastName { get; set; }
[JsonPropertyName("group")]
public string Group { get; set; }
[JsonPropertyName("state")]
public string State { get; set; }
}
public class Membership
{
public Member Member { get; set; }
public string Group { get; set; }
public string State { get; set; }

public Membership(JsonMember jsonMember){
Group = jsonMember.Group;
State = jsonMember.State;

Member = new Member{
Id = jsonMember.Id,
FirstName = jsonMember.FirstName,
LastName = jsonMember.LastName
};
}
}
public class Member
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

输出:

组:溢价

组:活跃的

成员Id: 123

成员名:James

成员姓氏:Smith

看:https://dotnetfiddle.net/y0i6Sx

通过在"Premium"后面添加逗号来修复json

"group": "Premium",

and try this

var json=...your json
var jD= JsonSerializer.Deserialize<Root>(json);
var memberShip = new Membership
{
Member = new Member { FirstName = jD.firstName, LastName = jD.lastName},
Group=jD.group,
State=jD.state
};
var output= JsonSerializer.Serialize(memberShip);

输出
{
"Member": {
"Id": 0,
"FirstName": "James",
"LastName": "Smith"
},
"Group": "Premium",
"State": "Active"
}

根类

public class Root
{
public int id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string group { get; set; }
public string state { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新