我有一个包含以下字段的课程.当需要调用外部rest API方法时,这些属性用于序列化为json对象.
public class Customer
{
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
[JsonProperty(PropertyName = "prop[listId]")]
public string Test{ get; set; }
// there are lot of properties
}
Run Code Online (Sandbox Code Playgroud)
在属性名称中Test,外部API服务调用需要一些像json文件名格式之类的东西.
prop[7]
Run Code Online (Sandbox Code Playgroud)
在我的情况下,这7可以根据test,dev和prod等环境进行更改.所以我正在寻找将listId值移动到app.config中的方法.
我试图按照以下方式执行此操作,但不允许这样做.因为listIdValue如果指定常量值,它将起作用.
private string listIdValue = ConfigurationManager.AppSettings["ListIdValue"];
[JsonProperty(PropertyName = "prop["+listIdValue +"]")]
public string Test{ get; set; }
Run Code Online (Sandbox Code Playgroud)
Mar*_*gal 11
您必须覆盖DefaultContractResolver并实现自己的机制来提供PropertyName(在JSON中).我将提供一个完整的示例代码,以显示运行时生成的反序列化和序列化PropertyName.目前,它将Test字段修改为Test5(在所有模型中).您应该实现自己的机制(使用属性,保留名称,表或其他.
class Program
{
static void Main(string[] args)
{
var customer = new Customer() {Email = "asd@asd.com", Test = "asdasd"};
var a = Serialize(customer, false);
var b = Serialize(customer, true);
Console.WriteLine(a);
Console.WriteLine(b);
var desA = Deserialize<Customer>(a, false);
var desB = Deserialize<Customer>(b, true);
Console.WriteLine("TestA: {0}", desA.Test);
Console.WriteLine("TestB: {0}", desB.Test);
}
static string Serialize(object obj, bool newNames)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (newNames)
{
settings.ContractResolver = new CustomNamesContractResolver();
}
return JsonConvert.SerializeObject(obj, settings);
}
static T Deserialize<T>(string text, bool newNames)
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (newNames)
{
settings.ContractResolver = new CustomNamesContractResolver();
}
return JsonConvert.DeserializeObject<T>(text, settings);
}
}
class CustomNamesContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
{
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
{
if (prop.UnderlyingName == "Test") //change this to your implementation!
prop.PropertyName = "Test" + 5;
}
return list;
}
}
public class Customer
{
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
public string Test { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
输出:
{
"email": "asd@asd.com",
"Test": "asdasd"
}
{
"email": "asd@asd.com",
"Test5": "asdasd"
}
TestA: asdasd
TestB: asdasd
Run Code Online (Sandbox Code Playgroud)
如您所见,当我们使用时Serialize(..., false)- 字段的名称是Test,当我们使用时Serialize(..., true)- 字段的名称是Test5,如预期的那样.这也适用于反序列化.
我用这个答案作为我的答案的检查:https://stackoverflow.com/a/20639697/773879
| 归档时间: |
|
| 查看次数: |
11264 次 |
| 最近记录: |