我用 uLkJSON ,怎么用解析json数据中的中文。

披着虎皮的石头 2013-07-16 06:38:30
我用 uLkJSON ,怎么用解析json数据中的中文。
当解析zh_desc的中文值时,显示为控制,而解析code、en_desc是对的。

  
var JSon_Root :TlkJSONobject; var Rlt_Str Rlt_Str :String;
Rlt_Str :='{"error_response":{"code":"22","zh_desc":"授权者不是商家","en_desc":"the franchisor not businessmen"}}'

JSon :=TlkJSON.ParseText(Rlt_Str) as TlkJSONobject;
if JSon.Field['error_response'] <>nil then begin
JSon_Root :=JSon.Field['error_response'] as TlkJSONobject;

with JSon_Root do begin
ShowMessage(getString('code'), ); //22
ShowMessage(getString('en_desc')); //the franchisor not businessmen
ShowMessage(Field['zh_desc'].Value); //为什么是空值,而不是授权者不是商家
end;
end;
...全文
924 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://www.cnblogs.com/cy163/archive/2007/05/31/766886.html UNICODE,GBK,UTF-8区别
  • 打赏
  • 举报
回复
忽发奇想,为什么中文要做成GBK、UTF_8两种格式,如果只有UTF_8格式, 那处理多简单啊。
  • 打赏
  • 举报
回复
我去官网下载了,替换了本地uLkJSON,发现是2007年版,官网下载是2009年10月, 所以早期的版本有中文解析错误,新版没有这个问题。
Y.A.K.E 2013-07-16
  • 打赏
  • 举报
回复
我用的是superobject.pas 很好用
sololie 2013-07-16
  • 打赏
  • 举报
回复
http://sourceforge.net/projects/lkjson/ uLkJSON 的官方下载,也就酱紫了
  • 打赏
  • 举报
回复
我又试了,还是显示不了中文,是不是UlkJson.pas是不同的版本。
  • 打赏
  • 举报
回复
我再 试试
sololie 2013-07-16
  • 打赏
  • 举报
回复
不是很简单的道理么,你 Rlt_Str := '{"error_response":{"code":"22","zh_desc":"授权者不是商家","en_desc":"the franchisor not businessmen"}}'; 创建的这个不是utf8编码的字符串,json解析时是按utf8编码解析的,所以你得用UTF8Encode进行utf8编码,看清楚这个是UTF8Encode编码不是UTF8Dncode解码 我是delphi7,看上图那么丑的UI就知道了
  • 打赏
  • 举报
回复
或者你看看uLkJson的源代码,是不是和我不一样,或者里面的代码修改过了。 uLkJson.pas我是从网上下载的。
  • 打赏
  • 举报
回复
我是在delphi7下运行uLkJson的,对话框没有出现中文值,你在delphi7下试试看。
sololie 2013-07-16
  • 打赏
  • 举报
回复
sololie 2013-07-16
  • 打赏
  • 举报
回复

var
Rlt_Str: string;
JSon, JSon_Root: TlkJSONobject;

valStr: string;
begin

Rlt_Str :=
'{"error_response":{"code":"22","zh_desc":"授权者不是商家","en_desc":"the franchisor not businessmen"}}';
Rlt_Str := UTF8Encode(Rlt_Str);
JSon := TlkJSONobject.Create;
JSon := TlkJSON.ParseText(Rlt_Str) as TlkJSONobject;
if JSon.Field['error_response'] <> nil then
begin
JSon_Root := JSon.Field['error_response'] as TlkJSONobject;

with JSon_Root do
begin
valStr := valStr + getString('code') + #10;
valStr := valStr + getString('en_desc') + #10;
valStr := valStr + (Field['zh_desc'] as TlkJSONstring).Value;
end;
end;

ShowMessage(valStr);
end;


  • 打赏
  • 举报
回复
你的意思是要转码为UTF-8以后,再用json去解析出来。
sololie 2013-07-16
  • 打赏
  • 举报
回复
你前面UTF8Dncode,解析的时候还得转回来 Rlt_Str :='{"error_response":{"code":"22","zh_desc":"授权者不是商家","en_desc":"the franchisor not businessmen"}}' Rlt_Str:=UTF8Encode(Rlt_Str);// 再utf8编码一次 // 再开始解析
  • 打赏
  • 举报
回复
我看了uLkJSon.pas的函数. 问题是怎么样,把WideString类型的value变量的中文值,正确读取。 TlkJSONstring = class(TlkJSONbase) protected FValue: WideString; function GetValue: Variant; override; procedure SetValue(const AValue: Variant); override; public procedure AfterConstruction; override; class function Generate(const wsValue: WideString = ''): TlkJSONstring; class function SelfType: TlkJSONtypes; override; class function SelfTypeName: string; override; end; function TlkJSONobject.getString(idx: Integer): String; var js:TlkJSONstring; begin js :=FieldByIndex[idx] as TlkJSONstring; if not assigned(js) then result := '' else result :=vartostr(js.Value); end;
  • 打赏
  • 举报
回复
用下面的语句,也是返回空值。ShowMessage(getString('zh_desc'));

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧