Search code examples
jsondelphidelphi-xe6system.json

Delphi XE 6 using System.JSON methods and implementations failing


I created a simple test app in Delphi XE6, I want to start using Delphi's JSON to handle requests from a custom coded web server (one I am busy with)

procedure TForm1.Button1Click(Sender: TObject);
var
  Servermethods : TServerMethods1Client;
  JsonArray : TJSONArray;
  JsonValue: TJSONValue;
  JSonObj: TJSONObject;
  JSPair: TJSONPair;
  s: String;
begin
  JSonObj := TJSONObject.Create;
  JSonObj.AddPair(TJSONPair.Create('a','abcde'));
  JsonArray := TJSONArray.Create;
  JsonArray.AddElement(JSonObj);

  JsonValue := JsonArray.Items[0];
  JSonObj := (JsonValue as TJSONObject);

  JSPair := TJSONPair(JSonObj);

  s := JSPair.JsonString.Value;
end;

When you Inspect/Evaluate any JSON object (CTRL+F7) there is simply no info, in the inspector I simply get "()" -[aka empty]-

But for arguments sake if I change the call to JSONObj.Tostring I get the full json string ({"a", "abcde"}) and thats cool thats cool but when it gets to the last line

s := JSPair.JsonString.Value;

Boom! Access Violation.

Any help would be appreciated

* Edit * What I'm actually asking i for someone to test this in XE6 to see if it's a bug? or am I missing something very obvious?


Solution

  • The JSONPair is part of the JSONObject, so you cannot cast the JSONObject to a JSONPair. Use JSONObject.Get to get the pair.