【问题标题】:ObjectToJsonString Capitalize First LetterObjectToJsonString 首字母大写
【发布时间】:2017-12-21 17:45:21
【问题描述】:

当涉及到字段名称的大写/小写时,我正在使用(IMO 不合理地)严格的 Web 服务。

我正在使用 TJSON.ObjectToJsonString 并且它正在小写所有字段名称。 ObjectToJsonString 环绕日期格式的所有选项,所以我有点茫然。

德尔福对象

TGeocodeServiceAddress = class(TInterfacedObject, IGeocodeServiceAddress)
private
  FAbbreviationFormat: String;
  FCountryPostalFilter: String;
  FCountry: String;
  FCounty: String;
  FState: string;
  FCity: string;
  FStreetAddress: String;
  FZip : String;
public
  {getters and setters here. Edited for brevity}

  property Zip: String read GetZip write SetZip;
  property AbbreviationFormat: String read GetAbbreviationFormat write SetAbbreviationFormat;
  property CountryPostalFilter: String read GetCountryPostalFilter write SetCountryPostalFilter;
  property Country: String read GetCountry write SetCountry;
  property County: String read GetCounty write SetCounty;
  property State: string read GetState write SetState;
  property City: string read GetCity write SetCity;
  property StreetAddress: String read GetStreetAddress write SetStreetAddress;
end;

JSON 字符串

{  
   "abbreviationFormat":"",
   "countryPostalFilter":"",
   "country":"USA",
   "county":"PIMA",
   "state":"AZ",
   "city":"TUCSON",
   "streetAddress":"6400 E. 6th St",
   "zip":"85711"
}

【问题讨论】:

  • 这是在TJSONConverter.ConvertFieldNameToJson 方法中硬编码的。您可以通过字段属性提供自定义名称,也可以编写自己的转换器。
  • 好的,谢谢您的信息。对于未来的那些,TJSONConverter.ConvertFieldNameToJson 在 REST.JsonReflect 单元中
  • @MartynA 我认为这是一个坏主意,StringReplace 将适用于整个 Json。也有转换价值数据的风险。一个简单的自动替换永远不会被信任。
  • @MartynA 是的,你是对的,但我不希望 Embarcadero 这样做
  • 就像 Victoria 说的,你可以使用 [JSONName] 属性告诉转换器所需的字段命名。无需编写自定义转换器。例如:[JSONName('AbbreviationFormat')] FAbbreviationFormat: String; [JSONName('CountryPostalFilter')] FCountryPostalFilter: String; ...

标签: json delphi delphi-10-seattle


【解决方案1】:

https://community.idera.com/developer-tools/programming-languages/f/delphi-language/69467/json---converting-array-of-custom-records-to-json-text

uses
...
  REST.Json.Types,
...

type
  CEItem= class
  public
    [JsonName('RowName')]
    RowName: String;
    [JsonName('Name')]
    Name: String;
    [JsonName('MaxStackSize')]
    MaxStackSize: FixedUInt;
    ...
  end;

【讨论】:

  • 谢谢,注意到必须添加 REST.Json.Types 以使用子句才能使其工作
猜你喜欢
  • 2021-07-29
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2021-11-02
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多