如何仅将字段名称序列化为数据成员 json?

How to serialize json with fields name as datamember only?

我有一个这样的 POCO:

public class Process
{
    public Process() { }

    [DataMember(Name = "lang_code")]
    public string LCode { get; set; }

    [DataMember(Name = "data_currency")]
    public string Currency { get; set; }

    [DataMember(Name = "country_code")]
    public string CCode { get; set; }

    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

现在,当我序列化我的 POCO 时,我得到 json 这样的字段名称:

{"LCode":"en-US","Currency":"USD","CCode":"IN"}

有什么方法可以像 DataMember 字段在序列化 POCO 之后那样获取它。如下所示:

{"lang_code":"en-US","data_currency":"USD","country_code":"IN"}

下面是我们的代码:

ProcessStr = ExtractHeader(headers, PROCESS_HEADER);
Console.WriteLine(ProcessStr);
if (!string.IsNullOrWhiteSpace(ProcessStr))
{
    Process = DeserializeJson<Process>(ProcessStr);
    if (Process != null && !string.IsNullOrWhiteSpace(Process.Gold))
    {
        Process.Gold = HttpUtility.HtmlEncode(Process.Gold);
    }
    ProcessStr = Process.ToString();
    Console.WriteLine(ProcessStr);
}

private T DeserializeJson<T>(string str) where T : new()
{
    try
    {
        return Utf8Json.JsonSerializer.Deserialize<T>(str);
    }
    catch (Exception e)
    {
        return new T();
    }
}

看起来您正在使用两个不同的包,Newtonsoft.Json 用于序列化,Utf8Json 用于反序列化。他们使用不同的注释。您可以让它工作,但选择一个或另一个可能更简单。

Newtonsoft.Json 使用 JsonProperty 属性,而 Utf8Json 使用 DataMember 属性。

using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Utf8Json;

namespace JSONPropertyTest
{
    public class Process
    {
        public Process() { }

        [JsonProperty("lang_code")]
        [DataMember(Name = "lang_code")]
        public string LCode { get; set; }

        [JsonProperty("data_currency")]
        [DataMember(Name = "data_currency")]
        public string Currency { get; set; }

        [JsonProperty("country_code")]
        [DataMember(Name = "country_code")]
        public string CCode { get; set; }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

    class Program
    {
        static private T DeserializeJson<T>(string str) where T : new()
        {
            try
            {
                return Utf8Json.JsonSerializer.Deserialize<T>(str);
            }
            catch (Exception e)
            {
                return new T();
            }
        }

        static void Main(string[] args)
        {
            var test = new Process { LCode = "en-US",Currency = "USD", CCode = "IN" };
            var json = test.ToString();

            Console.WriteLine($"serialized={test}");
            var deserialized = DeserializeJson<Process>(json);
            Debug.Assert(test.CCode == deserialized.CCode);
            Debug.Assert(test.LCode == deserialized.LCode);
            Debug.Assert(test.Currency == deserialized.Currency);
            Console.WriteLine($"deserialized={deserialized}");
        }
    }
}

要仅使用 Utf8Json,您需要更新 ToString 方法,这是您显示的代码中唯一依赖 Newtonsoft.Json 的方法。看起来像这样:

    public class Process
    {
        public Process() { }

        [DataMember(Name = "lang_code")]
        public string LCode { get; set; }

        [DataMember(Name = "data_currency")]
        public string Currency { get; set; }

        [DataMember(Name = "country_code")]
        public string CCode { get; set; }

        public override string ToString()
        {
            return Utf8Json.JsonSerializer.ToJsonString(this);
        }
    }