在属性名称前添加一些字符-JSON序列化 [英] Prefix some characters to the property name - JSON Serialization
本文介绍了在属性名称前添加一些字符-JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有类似的课程
class A
{
public int Age {get; set;}
public string Name {get; set;}
}
有没有一种方法可以序列化此对象以生成具有这样的任意字符的属性
Is there a way to serialize this object to generate properties with some arbitrary characters like this
[{ "prefix.age": 1, "prefix.name": "Apple" }]
我正在使用Newtonsoft.Json来满足我的序列化需求.
I'm using Newtonsoft.Json for my serialization needs.
推荐答案
只需应用 JsonProperty 属性添加到您的类属性中,如下所示:
Just apply the JsonProperty attribute to your class properties like this:
class A
{
[JsonProperty("prefix.age")]
public int Age { get; set; }
[JsonProperty("prefix.name")]
public string Name { get; set; }
}
然后将其序列化:
var a = new A { Age = 1, Name = "Apple" };
var serializedObject = JsonConvert.SerializeObject(a);
serializedObject
将包含JSON字符串:{"prefix.age":1,"prefix.name":"Apple"}
.
serializedObject
will contain JSON string: {"prefix.age":1,"prefix.name":"Apple"}
.
这篇关于在属性名称前添加一些字符-JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文