You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++Builder XE3的TDBGrid中正确显示JSON的Unicode编码字符串?

Unicode内容存入TDataSet后显示问号的问题

我使用Embarcadero C++Builder XE3搭配Indy的TIdHTTP组件,从API获取如下JSON响应:

{"Items":[{"Id":"jp-lp|o4nfz4IB6j9TcSaTQGxD","Type":"Container","Text":"106-0031 \u6771\u4EAC\u90FD \u6E2F\u533A \u897F\u9EBB\u5E03","Highlight":"0-3,4-8","Description":" - 991 Addresses"}]}

使用TJSONObject解析后,通过ShowMessage()可正常显示Text字段的Unicode内容:

106-0031 東京都 港区 西麻布

但将该值赋值给TDataSet(如TkbmMemTable)的TField的AsString或AsWideString属性后,ShowMessage()显示为问号,绑定到TDBGrid、TJvDBUltimGrid或TDBAdvGrid时也显示问号:

106-0031 ??? ?? ???

仅使用TStringGrid的Cells[][]直接赋值时能正常显示,但我必须通过TDataSet实现网格显示,试过多种数据集和转换方法均无效。相关代码及TkbmMemTable的dfm定义如下:

C++代码

String response = "{\"Items\":[{\"Id\":\"jp-lp|o4nfz4IB6j9TcSaTQGxD\",\"Type\":\"Container\",\"Text\":\"106-0031 \\u6771\\u4EAC\\u90FD \\u6E2F\\u533A \\u897F\\u9EBB\\u5E03\",\"Highlight\":\"0-3,4-8\",\"Description\":\" - 991 Addresses\"}]}";
TJSONObject *jsResponse = static_cast<TJSONObject *>(TJSONObject::ParseJSONValue(response));
String Key("Items");
TJSONArray *jsItems;
for(int i = 0; i < jsResponse->Size(); ++i){
  String CurrKey(jsResponse->Get(i)->JsonString->Value());
  if(CurrKey == Key){
    jsItems = ((TJSONArray *)(jsResponse->Get(i)->JsonValue));
    break;
  }
}
for (int i = 0; i < jsItems->Size(); ++i){
  TJSONObject* currItem = ((TJSONObject *)jsItems->Get(i));
  String correctlyDisplayedValue;
  for(int j = 0; j < currItem->Size(); ++j){
    String CurrKey(currItem->Get(j)->JsonString->Value());
    if(CurrKey == "Text"){
      correctlyDisplayedValue = ((TJSONString *)(currItem->Get(j)->JsonValue))->Value();
      break;
    }
  }
  ShowMessage(correctlyDisplayedValue); // 正常显示Unicode内容
  memoDS->Active = true;
  memoDS->FieldByName("Text")->AsWideString = correctlyDisplayedValue;
  ShowMessage(memoDS->FieldByName("Text")->AsWideString); // 显示问号
}

注:原代码存在变量名重复、未定义标识符的笔误,上述代码已修正以保证逻辑通顺。

TkbmMemTable的DFM定义

object memoDS: TkbmMemTable
  DesignActivation = True
  AttachedAutoRefresh = True
  AttachMaxCount = 1
  FieldDefs = <
    item
      Name = 'TEXT'
      DataType = ftWideString
    end>
  IndexDefs = <>
  SortOptions = []
  PersistentBackup = False
  ProgressFlags = [mtpcLoad, mtpcSave, mtpcCopy]
  LoadedCompletely = False
  SavedCompletely = False
  FilterOptions = []
  Version = '7.76.00 Standard Edition'
  LanguageID = 0
  SortID = 0
  SubLanguageID = 1
  LocaleID = 1024
  AutoUpdateFieldVariables = False
  Left = 566
  Top = 6
  object strngfldAddrSearchText: TStringField
    FieldName = 'TEXT'
    Size = 255
  end
end

内容的提问来源于stack exchange,提问作者Tomasz Sipel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 15:02:23