Json序列化变量PropertyName [英] Json serialization variable PropertyName

查看:131
本文介绍了Json序列化变量PropertyName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#和Newtonsoft.Json库对json进行序列化和反序列化.

I use c# and Newtonsoft.Json library for serialization and deserialization of json.

我有一个像这样的班

public class Animal
{
    [JsonProperty(PropertyName = "Dog")]
    public Key value {get;set;}
}

如果我将其实例化为

Animal a = new Animal{ Key = "bobby" };

我将其序列化,我将拥有一个json

and i serialize it i'll have a json like

{
    "dog": "bobby"
}

我可以动态更改序列化的PropertyName吗?例如,如果我想放"Bird"或"Cat"而不是"Dog"怎么办?

can i change the PropertyName of the serialization dynamically? for instance, what if i want to put "Bird" or "Cat" instead of "Dog"?

推荐答案

public class Animal
{
    public KeyValuePair<string,string> value {get;set;}
}

Animal a = new Animal { value = new KeyValuePair("dog","boddy")};

如果你想要鸟

Animal a = new Animal { value = new KeyValuePair("bird","bird1")};

这篇关于Json序列化变量PropertyName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆