我有一些包含内部属性的类,我想将它们序列化为json。我该如何实现?例如:
public class Foo
{
internal int num1 { get; set; }
internal double num2 { get; set; }
public string Description { get; set; }
public override string ToString()
{
if (!string.IsNullOrEmpty(Description))
return Description;
return base.ToString();
}
}
使用以下方式保存它:
Foo f = new Foo();
f.Description = "Foo Example";
JsonSerializerSettings settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
string jsonOutput = JsonConvert.SerializeObject(f, Formatting.Indented, settings);
using (StreamWriter sw = new StreamWriter("json_file.json"))
{
sw.WriteLine(jsonOutput);
}
我明白了。这是需要翻译的内容:I get
{
"$type": "SideSlopeTest.Foo, SideSlopeTest",
"Description": "Foo Example"
}
DefaultContractResolver
,以便它也能查看非公共属性。 - s.m.[JsonProperty()]
属性进行标记即可。 - dbc[JsonObject()]
进行标记。 - CAD bloke