JsonProperty - 使用不同的名称反序列化,而是用原来的名字进行序列化? [英] JsonProperty - Use different name for deserialization, but use original name for serialization?

查看:2516
本文介绍了JsonProperty - 使用不同的名称反序列化,而是用原来的名字进行序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个API检索JSON。我使用newtonsoft(这是json.net吧?)反序列化到对象列表这一点。有用。

I am retrieving JSON from an API. I am using newtonsoft (this is json.net right?) to deserialize this into a list of objects. It works.

不幸的是我还需要通过这一起给他人JSON(他们不能调用API直接只有我可以访问它)。我说,不幸的是,因为我需要输出我的JSON是从正在接收什么(属性名称必须是不同的)不同。

Unfortunately I also need to pass this along to someone else as JSON (they can't call the API directly only I have access to it). I say unfortunately because I need to OUTPUT my JSON that is different from what is being received (the property names need to be different).

例如,我有一个类所谓的人,有一个名为Name属性。我想人,所以我让我的请求,该API来获取JSON同上,找回人的名单。不幸的是,API不返回我的人与Name属性,它返回我PNAME。因此映射这一点,我只是做:

For example, I have a class called Person, with a property called Name. I want to get "People", so I make my request to that API to get JSON as above, and get back a list of Person. Unfortunately the API doesn't return me people with Name properties, it returns me pname. So to map this, I just do:

 [JsonProperty("pname")]

这是一个好主意 - 将其转换为PNAME名和我的课现在已经值了!我的人有名称的列表。

This is all well and good - it converts pname to name and my class now has the value! I have a list of people with names.

现在我需要给对象的这个列表返回给别人的名,但是当我序列化我的人类回JSON,它写出了JSON作为PNAME当我真的想将它写出来的名。我怀疑这是捡了JsonProperty。

Now I need to give this list of objects BACK to someone else as "Name", However when I serialize my people class back to JSON, it writes out the JSON as "pname" when I really want to write it out as "Name". I suspect it's picking up the "JsonProperty".

有没有只是还用它进行反序列化PNAME一种方式,但使用序列原始的属性值?

Is there a way to just have it use pname for deserialization, but use the original property value for serialization?

谢谢!

推荐答案

您可以创建自定义的合同解析器,设置该属性名回到您在C#类serilization之前定义的值。下面是一些示例代码;

You can create a custom contract resolver that sets the property names back to the ones you've defined in the C# class before serilization. Below is some example code;

class OriginalNameContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        // Let the base class create all the JsonProperties 
        IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);

        // assign the C# property name
        foreach (JsonProperty prop in list)
        {
            prop.PropertyName = prop.UnderlyingName;
        }

        return list;
    }
}

使用它像这样;

    JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.Formatting = Formatting.Indented;
    if (useLongNames)
    {
        settings.ContractResolver = new OriginalNameContractResolver();
    }

    string response = JsonConvert.SerializeObject(obj, settings);

这篇关于JsonProperty - 使用不同的名称反序列化,而是用原来的名字进行序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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