等效于.net5中的jarray.fromobject()?
我们有一个项目,该项目使用.net 5中使用 System.Text.json
而不是 newtonsoft jobign
。使用newtonsoft,替换动态JSON数据非常容易如下:如下所示:
siteDataObject["student"] = JArray.FromObject(studentservice.GetStudents());
sustomeService.getStudents()为返回列表如下所示,
internal class Student {
public int Id { get; set; }
public string Name { get; set; }
public string ContactPhone { get; set; }
public IEnumerable<MedicalRecord> MedicalRecords { get; set; }
}
internal class MedicalRecord {
public int Id { get; set; }
public string Name { get; set; }
public DateTime RecordDate { get; set; }
public IEnumerable<DiseaseLog> DiseaseLogs{ get; set; }
}
internal class DiseaseLog {
public int Id { get; set; }
public string Name { get; set; }
public DateTime LogDate { get; set; }
}
但在system.text.json中,
foreach (var element in doc.RootElement.EnumerateObject()) {
if (element.Name == "student") {
writer.WritePropertyName(element.Name);
}
else {
element.WriteTo(writer);
}
}
我不知道如何转换 list&lt; student&lt; ;
进入JSON数组数据时,当学生类具有许多内部包含多收集的属性时。
Can anyone advise how to convert it ?
为了澄清,我需要为此提出完整的代码,我有一个动态的JSON字符串,想替换元素:学生将代码替换为新的记录,
var dynamicJson = @"{'roomid':1,'roomcode':'Code001','students':[1],'contentdata':'say hello','footerdata':'cookie policy'}";
using MemoryStream stream = new MemoryStream();
using Utf8JsonWriter writer = new Utf8JsonWriter(stream);
using var dynamicDocument = JsonDocument.Parse(dynamicJson);
writer.WriteStartObject();
foreach (var element in dynamicDocument.RootElement.EnumerateObject())
{
if (element.Name == "students")
{
// unknown how to modify the student record into array
}
else
element.WriteTo(writer);
}
writer.WriteEndObject();
stream.Flush();
var modifyJson = Encoding.UTF8.GetString(stream.ToArray());
如果学生元素是字符串,我将知道如何修改学生的价值,但是我don't know how to modify it into array, by using simple code.作为学生在里面有多个课程。
我的预期结果应该是
{
"roomid": 1,
"roomcode": "Code001",
"students": [
{
"id": 1,
"Name": "Wilson",
"ContactPhone": "123-122-3311",
"MedicalRecords": [
{
"id": 101,
"Name ": "Medial record 101011",
"RecordDate": "2021-12-31",
"DiseaseLogs": [
{
"id": 18211,
"Name ": "Patient Log 19292",
"LogDate": "2020-1-31"
},
{
"id": 18212,
"Name ": "Patient Log 2911w",
"LogDate": "2020-3-31"
}
]
}
]
}
],
"contentdata": "say hello",
"footerdata": "cookie policy"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在.NET 5中,没有内置到System.Text.json中的可修改JSON文档对象模型。 是仅读取的,
system.text.json.nodes
仅在.NET 6中引入。因此,最简单的方法是供应,修改和重新序列化的自由形式JSON <强>在.NET 5中是对某些部分数据模型进行序列化,并将未知值绑定到字典中。如果您不关心根级的属性顺序,则可以使用
公共对象学生{get;放; }
属性,并将其余元素绑定到jsonextensiondata
溢出字典:然后deleialize,修改和重新序列化如下:
哪些结果
必须在
susidenite
属性中声明为属性为<代码>对象因为输入JSON已经具有包含单个整数值的数组。将其宣布为公共列表&lt; student&gt;学生{get;放; }
在最初加载JSON时会导致避难所。演示小提琴#1 在这里。
如果您确实关心根级的属性顺序,则可以对
orderedDictionary
(旧订单 - 订单 - 列出的非基因词典。 “ 值,然后重新序列化:将导致
演示小提琴#2 在这里。
在.net 6中,通过使用
system.text.json.nodes
可编辑的JSON文档对象模型:但是在.NET 5中,这是不可能的。演示小提琴#3 在这里。
In .NET 5 there is no modifiable JSON Document Object Model built into to System.Text.Json.
JsonDocument
is read-only, andSystem.Text.Json.Nodes
was only introduced in .NET 6. Thus, the easiest way to deserialize, modify and re-serialize free-form JSON in .NET 5 is to deserialize to some partial data model, with unknown values bound into a dictionary.If you do not care about the order of properties at the root level, you could deserialize to a model with a
public object students { get; set; }
property, and bind the remaining elements to aJsonExtensionData
overflow dictionary:Then deserialize, modify and re-serialize as follows:
Which results in
the
students
property must be declared asobject
because the input JSON already has an array containing a single integer value; declaring it aspublic List<Student> students { get; set; }
would result in a deserialization when initially loading the JSON.Demo fiddle #1 here.
If you do care about the order of properties at the root level, you could deserialize to an
OrderedDictionary
(an old order-preserving non-generic dictionary dating from .NET Framework 2.0 which is still around and supported), overwrite the"students"
value, and re-serialize:Which results in
Demo fiddle #2 here.
In .NET 6 this all becomes easier through use of the
System.Text.Json.Nodes
editable JSON Document Object Model:But in .NET 5 this is not possible. Demo fiddle #3 here.
我认为这就是您想要的(数组或单个嵌套对象):
I think this is what you want (array or single nested object):