0

I got the following code:

procedure TForm2.Button1Click(Sender: TObject);
Const
StrJson=
'{'+
'    "products": {'+
'        "Men''s Sneakers": {'+
'            "instock": false,'+
'            "size": "423",'+
'            "manufacturer": "阿迪达斯",'+
'            "lastcheck": "20120529"'+
'        },'+
'        "Purse": {'+
'            "instock": true,'+
'            "size": "not applicable",'+
'            "manufacturer": "普拉达",'+
'            "lastcheck": "20120528"'+
'        },'+
'        "Men''s Hood": {'+
'            "instock": false,'+
'            "size": "M",'+
'            "manufacturer": "通用",'+
'            "lastcheck": "20120529"'+
'        }'+
'    },'+
'    "total": 41,'+
'    "available": 30'+
'}';
var
  LJsonObj  : TJSONObject;
  LJPair    : TJSONPair;
  LProducts : TJSONValue;
  LProduct  : TJSONValue;
  LItem     : TJSONValue;
  LIndex    : Integer;
  LSize     : Integer;
begin
  LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
  try
     LProducts := LJsonObj.Get('products').JsonValue;
     LSize := TJSONArray(LProducts).Size;
     for LIndex:=0 to LSize-1 do
     begin
      LProduct := TJSONArray(LProducts).Get(LIndex);
      LJPair   := TJSONPair(LProduct);
      memo1.lines.add(Format('Product Name %s',[LJPair.JsonString.Value]));
        for LItem in TJSONArray(LJPair.JsonValue) do
        begin
           if TJSONPair(LItem).JsonValue is TJSONFalse then
            memo1.lines.add(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'false']))
           else
           if TJSONPair(LItem).JsonValue is TJSONTrue then
            memo1.lines.add(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'true']))
           else
            memo1.lines.add(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
        end;
     end;
  finally
     LJsonObj.Free;
  end;
end;

It proceduces the following result:

Product Name Men's Sneakers
  instock : false
  size : 423
  manufacturer : ????
  lastcheck : 20120529
Product Name Purse
  instock : true
  size : not applicable
  manufacturer : ???
  lastcheck : 20120528
Product Name Men's Hood
  instock : false
  size : M
  manufacturer : ??
  lastcheck : 20120529

My question is how do i get the chinese characters parsed out instead of the ??? symbols. Is there some setting i am missing?

  • 1
    You clearly have not even read the code. Never mind attempted to understand it. Had you read it, you would have seen the word `ASCII`. If you don't know what ASCII means then you are in too deep. If you didn't even read the code before using it, and asking this question, then that is a sign of laze. In order to be successful at programming you have to learn to delve deeply into a problem. – David Heffernan Sep 10 '13 at 08:34

1 Answers1

3

It is obvious because you are using TEncoding.ASCII.GetBytes. You should use TEncoding.UTF8.GetBytes.

You can also use the TJSONObject.ParseJSONValue overload that takes a string directly.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
  • 5
    Copy paste programming is getting worse... Step 1 - copy code from SO (http://stackoverflow.com/a/10809685/327083), Step 2 - paste into user application, edit; Step 3 - copy broken edits to SO; Step 4 paste solution into user application; rinse, repeat. I wonder how many applications SO answers have unknowingly written piecemeal in this way. – J... Sep 10 '13 at 08:14
  • 2
    @J... Yes, why bother trying to understand anything when you can use the programming technique known as "inheriting from the clipboard". And if it doesn't work you can just ask a question on SO and let somebody else do the understanding. – David Heffernan Sep 10 '13 at 08:33