JSON object

Web Core newby here...

I am having trouble building a TJSONObject with a simple key-value pair...

uses
  WebLib.JSON;

var
  JO: TJSONObject;
  HelloValue: String;
  JsonString: String;

begin
  JO := TJSONObject.Create;
  JO.AddPair(TJSONPair.Create('hello', 'world'));
  JsonString := JO.ToJson;    // This works fine!
  HelloValue := JO.GetValue('hello').ToString;   // This does not work!?
end;

The value of JsonString calculates correctly as {"hello":"world"}. The GetValue statement, however, fails with an "ncaught TypeError: Cannot read property 'hello' of null".

I must be missing something obvious, but what?

Thanks for your response.

JSON handling in JavaScript is slightly different. In the browser performing an AddPair() cannot modify the object as JSON object itself.

We have done an improvement to make this easier in the following way:

  JO := TJSONObject.Create;
  jv := JO.AddPair(TJSONPair.Create('hello', 'world'));
  HelloValue := Jv.GetValue('more').ToString;  


Thank you Bruno. I think it makes sense. Sort of ;)

Bruno, I have now tried your code example, and get the exact same error as before for GetValue(). Introducing the new JV object does not seem to make any difference?


PS: Your corde does a GetValue() on a 'more' key. when you meant 'hello' ;)