在C#中,我正在构建一个类(简化为讨论目的),最终将被序列化为一些外部定义的
JSON:
{ "$schema": "http://example.com/person.json","name": "John","age": 86 }
在我的代码中,我会有一些像:
public class Person { public const string $schema= @"http://example.com/person.json"; public string name {get;set; } public int age {get; set;} }
…
Person person = new Person(); person.name = "John"; person.age = 88; JavaScriptSerializer serializer = new JavaScriptSerializer(); string json = serializer.Serialize(person);
解决方法
提供属性[DataContract]到你的Person类.
另外,你是否意味着使模式const?
[DataContract] public class Person { [DataMember(Name = "$schema")] public string schema { get; set; } public string name { get; set; } public int age {get; set;} }