【发布时间】:2021-01-10 20:26:41
【问题描述】:
我一直在尝试使用 JSON Patch 在嵌套对象中使用替换值,但是我觉得我没有得到正确的表示法。任何想法应该是什么路径?
我在LINQPad 6 中创建了以下代码来证明它。
void Main()
{
var patchTest = new PatchTest();
patchTest.Create();
patchTest.ToString().Dump("Before Patch");
var patch = JsonConvert.DeserializeObject<JsonPatchDocument<Contact>>(
@"[
{
""op"": ""replace"",
""path"": ""/firstname"",
""value"": ""Benjamin""
},
{
""op"": ""replace"",
""path"": ""age"",
""value"": ""29""
},
{
""op"": ""replace"",
""path"": ""//Appointment//Name"",
""value"": ""fsdfdsf""
},
]");
patchTest.Patch(patch);
patchTest.ToString().Dump("After Patch");
}
public class PatchTest
{
public Contact Contact { get; set; }
public PatchTest() { }
public void Create()
{
Contact = new Contact
{
FirstName = "Walt",
LastName = "Banks",
Age = 20
};
}
public void Patch(JsonPatchDocument<Contact> patch)
{
patch.Replace(e => e.Appointment, Contact.Appointment);
patch.ApplyTo(Contact);
}
public override string ToString()
{
return $"{nameof(Contact)}: {Contact}";
}
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Appointment Appointment { get; set; }
public override string ToString()
{
return $"{nameof(FirstName)}: {FirstName}, {nameof(LastName)}: {LastName}, {nameof(Appointment)}: {Appointment}";
}
}
public class Appointment
{
public string Name { get; set; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}";
}
}
但是找不到名字
【问题讨论】:
标签: c# json asp.net-core json-patch