zoukankan      html  css  js  c++  java
  • 如何解析DELPHI XE5服务器返回的JSON数据(翻译)及中文乱码

    [plain] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <span style="font-size:14px;">一直想找如何解析JSON数据的说,今天终于找到有人发帖子了。之前有人说用superobject,Tlkjson,delphi json library,delphi  web unit等等。其实我是想找比较简单的解析方式。解析简单的json。下面是转载的坦然的源码。  
    2. </span>  
    [delphi] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. unit Unit1;  
    2.   
    3. interface  
    4.   
    5. uses  
    6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  
    7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,DBXJSON;  
    8.   
    9. type  
    10.   TForm1 = class(TForm)  
    11.     Button1: TButton;  
    12.     procedure Button1Click(Sender: TObject);  
    13.   private  
    14.     { Private declarations }  
    15.   public  
    16.     { Public declarations }  
    17.   end;  
    18.   
    19. var  
    20.   Form1: TForm1;  
    21.   
    22. implementation  
    23.   
    24. {$R *.dfm}  
    25.   
    26. const  
    27.   GJSONString =  
    28.     '{' +  
    29.     '    "name": {'+  
    30.     '        "A JSON Object": {' +  
    31.     '          "id": "1"' +  
    32.     '        },' +  
    33.     '        "Another JSON Object": {' +  
    34.     '          "id": "2"' +  
    35.     '        }' +  
    36.     '    },' +  
    37.     '    "totalobjects": "2"' +  
    38.     '}';  
    39.   
    40. procedure TForm1.Button1Click(Sender: TObject);  
    41. var  
    42.   LJSONObject: TJSONObject;  
    43.   Value: TJSONValue;  
    44. begin  
    45.   LJSONObject := nil;  
    46.   try  
    47.     LJSONObject := TJsonObject.Create;  
    48.     Value := TJSONValue.Create;  
    49.     { convert String to JSON }  
    50.     LJSONObject.Parse(BytesOf(GJSONString), 0);  
    51.     Value :=LJSONObject.GetValue('name');  
    52.     ShowMessage(Value.ToString);  
    53.   finally  
    54.     LJSONObject.Free;  
    55.   end;  
    56. end;  
    57.   
    58. end.  


    灰常好,在此谢谢博主。

    但是这样处理中文的时候会出现乱码现象。我对代码稍微修改了一下:

    [delphi] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. var   
    2.   jo:tjsonobject;  
    3.   jv:tjsonvalue;  
    4.   jsonstr:string;//要转换的json字符串  
    5. begin  
    6.   jo:=nil;  
    7.   jsonstr:='{"name":"流川枫","interest":"与樱木吵架"};  
    8.    try  
    9.       jo:=tjsonobject.create;  
    10.       jo:=tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr),0) as tjsonobject;  
    11.       jv:=jo.get('interest').jsonvalue;  
    12.       showmessage(jv.value);  
    13.    finally  
    14.     jo.Free;  
    15.    end;  
    16. end;   

    终于能转换成中文了。
    下面是关于jsonobject的解析(举一反三):

    [delphi] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. procedure TForm1.Button1Click(Sender: TObject);  
    2. var  
    3.   jsonstr: string;  
    4.   jvalue: tjsonvalue;  
    5.   jobj: tjsonobject;  
    6.   jpair: tjsonpair;  
    7.   jarray: tjsonarray;  
    8. begin  
    9.     
    10.   jsonstr:='{'name':'tom','password':'tomcat','interests':['mouse','meat']}';  
    11.   jvalue := tjsonobject.ParseJSONValue  
    12.     (tencoding.UTF8.GetBytes(jsonstr), 0);  
    13.   try  
    14.     jobj := jvalue as tjsonobject;  
    15.     jpair := jobj.Get(2); // get the third json pair  
    16.     jarray := jpair.JsonValue as tjsonarray; // pair value is an array ['mouse','meat']  
    17.     strresult := jarray.Get(0).value; // first element of array['mouse','meat']  
    18.     showmessage(strresult);//it is mouse  
    19.   finally  
    20.     jvalue.Free;   
    21.   end;  
    22. end;   
     
    http://blog.csdn.net/syndicater/article/details/17371111
  • 相关阅读:
    WEB 应用缓存解析以及使用 Redis 实现分布式缓存
    MVC 架构模式
    用两个栈实现队列
    重建二叉树
    从尾到头打印链表
    替换空格
    二维数组中的查找
    二叉树与线索二叉树
    OpenYurt:延伸原生 Kubernetes 到边缘场景下的落地实践
    开放下载!解锁 Serverless 从入门到实战大“橙”就
  • 原文地址:https://www.cnblogs.com/findumars/p/6358510.html
Copyright © 2011-2022 走看看