Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 简单编组解组对象_Delphi_Json_Serialization_Marshalling_Unmarshalling - Fatal编程技术网

Delphi 简单编组解组对象

Delphi 简单编组解组对象,delphi,json,serialization,marshalling,unmarshalling,Delphi,Json,Serialization,Marshalling,Unmarshalling,json支持是Delphi2009和Delphi2010的新特性之一。我想知道是否有任何简单的函数可以直接在字符串和对象之间进行编组/解编组,就像在superobject库中一样 例如: MyKnownObject := FromJSON('{name:"francis", surname:"lee"}'); 看。下面是有趣的部分: procedure TForm13.Button4Click(Sender: TObject); var LContact: TContact; oMar

json支持是Delphi2009和Delphi2010的新特性之一。我想知道是否有任何简单的函数可以直接在字符串和对象之间进行编组/解编组,就像在superobject库中一样

例如:

MyKnownObject := FromJSON('{name:"francis", surname:"lee"}');
看。下面是有趣的部分:

procedure TForm13.Button4Click(Sender: TObject);
var
  LContact: TContact;
  oMarshaller: TJSONMarshall;
  crtVal: TJSONValue;
begin
  LContact:=TContact.Create; //our custom class
  LContact.Name:='wings-of-wind.com';
  LContact.Age:=20; //fill with some data
  oMarshaller:=TJSONMarshal.Create(TJSONConverter.Create); //our engine
  try
    crtVal:=oMarshaller.Marshal(LContact); //serialize to JSON
    Memo1.Text:=crtVal.ToString; //display
  finally //cleanup
    FreeAndNil(LContact);
    FreeAndNil(oMarshaller);
  end;
end;

您还可以看到Adrian Andrei(DataSnap架构师)的一个更复杂的示例以及一个自定义封送的示例。

将字符串直接反序列化到
TJSONObject

var
  ConvertFrom: String;
  JSON: TJSONObject;
  StringBytes: TBytes;
  I: Integer;
begin
  ConvertFrom := '{"name":"somebody on SO","age":"123"}';
  StringBytes := TEncoding.ASCII.GetBytes(ConvertFrom);
  JSON := TJSONObject.Create;
  try
    JSON.Parse(StringBytes, 0);
    Assert(JSON.ToString = ConvertFrom, 'Conversion test');
    Memo1.Lines.Add(JSON.ToString);

    for I := 0 to JSON.Size - 1 do
      Memo1.Lines.Add(JSON.Get(I).JsonString.Value + 
         ' : ' + JSON.Get(I).JsonValue.Value);

  finally
    JSON.Free;
  end;
end;

我知道D2010中内置了对JSON编组的支持,但我不确定它到底是如何工作的。那么解组呢?@Francis unmarshalling在链接的文章中进行了讨论。@skamradt示例从TJsonValue而不是字符串解组。第一个链接已断开。请注意self(和其他人):REST.JsonReflect中还定义了一个TJSONMarshal/TJSONUnMarshal类。不要使用它,而是使用DBXJSONReflect中的类!