C#-在JSON序列化和反序列化之间设置不同的PropertyName [英] C# - Set different PropertyName between JSON serialization and deserialization

查看:818
本文介绍了C#-在JSON序列化和反序列化之间设置不同的PropertyName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询API,响应是自定义JSON.浏览完网络后,我设法通过以下方式将其反序列化为自定义对象:

I'm querying an API, the response is a custom JSON. After looking over the web, I managed to deserialize it into a custom object this way:

Post.cs

[JsonConverter(typeof(JsonPathConverter))]
public class Post
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "taken_at_timestamp")]
    public long Timestamp { get; set; }

    [JsonProperty(PropertyName = "edge_media_preview_like.count")]
    public int LikeCount { get; set; }

    [JsonProperty(PropertyName = "edge_media_to_comment.count")]
    public int CommentCount { get; set; }

    [JsonProperty(PropertyName = "edge_media_to_caption.edges[0].node.text")]
    public string Caption { get; set; }

    [JsonProperty(PropertyName = "display_url")]
    public string Image { get; set; }

    [JsonProperty(PropertyName = "dimensions.width")]
    public int Width { get; set; }

    [JsonProperty(PropertyName = "dimensions.height")]
    public int Height { get; set; }

    [JsonProperty(PropertyName = "shortcode")]
    public string Shortcode { get; set; }
}

JsonPathConverter.cs

public class JsonPathConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        object targetObj = Activator.CreateInstance(objectType);

        foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite))
        {
            JsonPropertyAttribute att = prop.GetCustomAttributes(true)
                                            .OfType<JsonPropertyAttribute>()
                                            .FirstOrDefault();

            string jsonPath = (att != null ? att.PropertyName : prop.Name);
            JToken token = jo.SelectToken(jsonPath);

            if (token != null && token.Type != JTokenType.Null)
            {
                object value = token.ToObject(prop.PropertyType, serializer);
                prop.SetValue(targetObj, value, null);
            }
        }

        return targetObj;
    }

    public override bool CanConvert(Type objectType)
    {
        return false;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

问题是,当我想将该对象作为JSON发送时:它返回具有PropertyName值而不是属性名称的字段:

The problem is when I want to send that object as a JSON: It returns fields with PropertyName value, instead of attribute name:

[
  {
    "id": "2322829435103312153",
    "taken_at_timestamp": 1591122868,
    "edge_media_preview_like.count": 24,
    "edge_media_to_comment.count": 0,
    "edge_media_to_caption.edges[0].node.text": "...",
    "display_url": "...",
    "dimensions.width": 1080,
    "dimensions.height": 1080,
    "shortcode": "..."
  }
  ...
]

是否可以使用PropertyName进行反序列化,而使用属性名进行序列化?

Is there a way to use PropertyName for deserialize but attribute name for serialize?

更新

根据要求,这就是我使用反序列化和序列化的方式:

As requested, this is how I use deserialization and serialization:

反序列化

JArray response = "[{ ... }]";

List<Post> posts = new List<Post>();

foreach (JObject node in response)
{
    Post post = JsonConvert.DeserializeObject<Post>(node.ToString());
    posts.Add(post);
}

序列化

我只是将 List 返回到确定"框中, IActionResult :

I just return the List inside an "Ok" IActionResult:

Ok(posts);

推荐答案

我通过WriteJson方法将其解决为 Chetan Ranpariya 建议:

I solved it through WriteJson method as Chetan Ranpariya suggested:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    JArray result = new JArray();

    if (value.GetType().IsGenericType && value is IEnumerable)
    {
        IEnumerable list = value as IEnumerable;

        foreach(var obj in list)
        {
            result.Add(GetObjectJson(obj));
        }
    }

    result.WriteTo(writer);
}

private JObject GetObjectJson(object obj)
{
    JObject jObj = new JObject();
    PropertyInfo[] props = obj.GetType().GetProperties();

    foreach (PropertyInfo prop in props)
    {
        if (!prop.PropertyType.ToString().Contains("System"))
            jObj.Add(char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1), GetObjectJson(prop.GetValue(obj)));
        else
            jObj.Add(char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1), JToken.FromObject(prop.GetValue(obj)));
    }

    return jObj;
}

就像在object value参数中一样,我接收到对象(在这种情况下,它是一个列表),然后将其强制转换并对其对象进行迭代.

As in object value parameter I receive the object (which is a list, in this case) I cast it and iterate over it's objects.

由于每个对象都可以具有原始和非原始属性,因此我创建了一个单独的函数以在发现嵌套对象的情况下递归使用它.

Since every object can have primitive and non primitive properties, I created a separated function to use it recursively in case I found a nested object.

这篇关于C#-在JSON序列化和反序列化之间设置不同的PropertyName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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