How to parse the JSON data returned by Delphi XE5 Server (translation) and Chinese garbled characters

Source: Internet
Author: User
Tags convert string to json string to json

[Plain]View PlainCopyprint?
    1. <span style= "FONT-SIZE:14PX;" > has been looking for how to parse the JSON data, today finally found someone posted. It was previously said to use Superobject,tlkjson,delphi JSON Library,delphi Web unit and so on. In fact, I'm looking for a simpler way to parse. Parse the simple JSON. The following is a reprint of the calm source.
    2. </span>
[Delphi]View PlainCopyprint?
  1. Unit Unit1;
  2. Interface
  3. Uses
  4. Winapi. Windows, Winapi. Messages, System. Sysutils, System. Variants, System. Classes, Vcl. Graphics,
  5. VCL. Controls, Vcl. Forms, Vcl. Dialogs, Vcl.  Stdctrls,dbxjson;
  6. Type
  7. TForm1 = Class (Tform)
  8. Button1:tbutton;
  9. procedure Button1Click (sender:tobject);
  10. Private
  11. {Private declarations}
  12. Public
  13. {public declarations}
  14. end;
  15. Var
  16. Form1:tform1;
  17. Implementation
  18. {$R *.DFM}
  19. Const
  20. Gjsonstring =
  21. ' {' +
  22. ' ' name ': {' +
  23. ' A JSON Object ': {' +
  24. ' id ': ' 1 ' +
  25. '}, ' +
  26. ' Another JSON Object ': {' +
  27. ' id ': ' 2 ' +
  28. '} ' +
  29. '}, ' +
  30. ' "Totalobjects": "2" ' +
  31. '} ';
  32. Procedure TForm1.  Button1Click (Sender:tobject);
  33. Var
  34. Ljsonobject:tjsonobject;
  35. Value:tjsonvalue;
  36. Begin
  37. Ljsonobject: = nil;
  38. Try
  39. Ljsonobject: = Tjsonobject.  Create;
  40. Value: = Tjsonvalue.  Create;
  41. {convert String to JSON}
  42. Ljsonobject.  Parse (Bytesof (gjsonstring), 0);
  43. Value: =ljsonobject.  GetValue (' name ');
  44. ShowMessage (Value.  ToString);
  45. finally
  46. Ljsonobject.  Free;
  47. end;
  48. End
  49. End.


Gray often good, here thank Bo Master.

However, there will be garbled characters when dealing with Chinese in this way. I modified the code a little bit:

[Delphi]View PlainCopy print?
  1. Var
  2. Jo:tjsonobject;
  3. Jv:tjsonvalue;
  4. JSONSTR:string; The JSON string to convert
  5. Begin
  6. jo:=Nil;
  7. Jsonstr:= '{"name": "Chuan Feng", "interest": "Quarrel with Cherry Wood"};
  8. Try
  9. Jo:=tjsonobject. Create;
  10. Jo:=tjsonobjectparsejsonvalue (tencoding. UTF8GetBytes (jsonstr),0) as tjsonobject;
  11. Jv:=jo. Get (' interest '). Jsonvalue;
  12. ShowMessage (JV. Value);
  13. finally
  14. Jo.  Free;
  15. end;
  16. End

can finally be converted into Chinese.
The following is an analysis of Jsonobject (extrapolate):

[Delphi]View PlainCopyprint?
  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. jsonstr:=' {' name': ' Tom', ' Password': ' Tomcat', ' Interests': [' Mouse', ' Meat']} ';
  10. Jvalue: = Tjsonobject. Parsejsonvalue
  11. (Tencoding. UTF8.  GetBytes (jsonstr), 0);
  12. Try
  13. Jobj: = Jvalue as tjsonobject;
  14. Jpair: = Jobj. Get (2); //Get the third JSON pair
  15. Jarray: = Jpair. Jsonvalue as Tjsonarray; //Pair value is an array [' mouse ', ' meat ']
  16. Strresult: = Jarray. Get (0). value; //First element of array[' mouse ', ' meat ']
  17. ShowMessage (strresult); //it is mouse
  18. finally
  19. Jvalue.   Free;
  20. end;
  21. end;
http://blog.csdn.net/syndicater/article/details/17371111

How to parse the JSON data returned by Delphi XE5 Server (translation) and Chinese garbled characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.