0

我得到以下代码:

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;

它产生以下结果:

产品名称 男士运动鞋
  库存:假
  尺寸:423
  制造商 : ????
  最后检查:20120529
产品名称钱包
  库存:真实
  尺寸:不适用
  制造商 : ???
  最后检查:20120528
产品名称男士风帽
  库存:假
  尺寸:M
  制造商 : ??
  最后检查:20120529

我的问题是我如何得到汉字而不是 ??? 符号。我缺少一些设置吗?

4

1 回答 1

3

这很明显,因为您使用的是TEncoding.ASCII.GetBytes. 你应该使用TEncoding.UTF8.GetBytes.

您还可以使用TJSONObject.ParseJSONValue直接采用字符串的重载。

于 2013-09-10T07:35:24.440 回答