F#类型JSON是输出名称@和名称
我使用F#类型,并用它在WPF窗口 的数据源,也产生从它的JSON:F#类型JSON是输出名称@和名称
type Customer = { mutable Name: string ;
mutable Phone: PhoneNumber list
}
and
PhoneNumber = {
mutable Number: string
}
但输出是这:
{"[email protected]":"Superhero","[email protected]":[{"[email protected]":"111","Number":"111"},{"[email protected]":"222","Number":"222"},{"[email protected]":"333","Number":"333"}],"Name":"Superhero","Phones":[{"[email protected]":"111","Number":"111"},{"[email protected]":"222","Number":"222"},{"[email protected]":"333","Number":"333"}]}
如果我改变类型:
type Customer = { Name: string ; Telephone: string }
它输出的Json我希望:
{"Name":"Superhero","Phones":[{"Number":"111"},{"Number":"222"},{"Number":"333"}]}
但WPF绑定抛出此异常:
一双向或OneWayToSource结合不能对只读属性
这项工作是我用来生成的代码Json C#:
var phonelist = new List<PhoneNumber> { new PhoneNumber("111"), new PhoneNumber("222"),
new PhoneNumber("333")
};
var fsharpList = Microsoft.FSharp.Collections.ListModule.OfSeq(phonelist); // TODO: Use a Extension Method
var c = new EntidadesFS.DO.Customer(name: "Superhero", phones: fsharpList);
var json = Newtonsoft.Json.JsonConvert.SerializeObject(c);
为什么会发生这种情况?
如何仅输出属性名称,使用可变,没有@末尾?
或如何解决这个问题?
回答:
基础上的宝贵意见对这个问题,并与一些更多的搜索
我能够找到一个解决方案:更好
[<CLIMutable>] [<JsonObject(MemberSerialization = MemberSerialization.OptIn)>]
type Customer = {
[<JsonProperty>] mutable Name: string
[<JsonProperty>] mutable Phones: PhoneNumber list
}
and [<CLIMutable>]
[<JsonObject(MemberSerialization = MemberSerialization.OptIn)>]
PhoneNumber = {
[<JsonProperty>] mutable Number: string
}
,或者甚至,如果我继续等领域不可变的(F#默认) ,
我只需要添加[<CLIMutable>]属性:
[<CLIMutable>] type Customer = {
Name: string
Phones: PhoneNumber list
}
and
[<CLIMutable>]
PhoneNumber = {
Number: string
}
输出:
{"Name":"Superhero","Phones":[{"Number":"111"},{"Number":"222"},{"Number":"333"}]}
现在,这产生的Json预期,并且也与WPF绑定效果很好。
以上是 F#类型JSON是输出名称@和名称 的全部内容, 来源链接: utcz.com/qa/257521.html