8

Trying to understand the JSON in Delphi. Using the module "DBXJSON.pas". How to use it to make this such an array:

Array:[
        {"1":1_1,"1_2_1":1_2_2},
        ...,
   ]

Doing so:

JSONObject:=TJSONObject.Create;
JSONArray:=TJSONArray.Create();
...
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1','1_1')));
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1_2_1','1_2_2')));
JSONObject.AddPair('Array',JSONArray);

, but get this:

{
"Array":[
{"1":"1_1"},{"1_2_1":"1_2_2"}
]
}

Please help! Thanks!

dedoki
  • 709
  • 4
  • 14
  • 24
  • 1
    There are obvious advantages to using whatever is shipped with Delphi, but if you're going to do a lot of JSON stuff, you should really take a look at SuperObject. It's awesome in so many ways. http://code.google.com/p/superobject/source/checkout – Wouter van Nifterick May 11 '12 at 15:39
  • You claim to want a value in your object of `1_1`. That's not a valid Javascript value. Numeric literals aren't allowed to contain underscores. (They're allowed in Java and Perl, but not Javascript.) Please clarify what you really wanted, because as your question currently appears, what you want is not really JSON at all, so you shouldn't be using a JSON library. – Rob Kennedy May 11 '12 at 21:01

2 Answers2

12

Code, wich you posted above, is not correct. You've created an JSON-Array and trying to add pair-elements to that array. But, instead of adding pairs to array you have to add JSON Objects to this array, and these objects have to contain your pairs.
here is an sample code to solve your problem:

program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils, dbxjson;

var jsobj, jso : TJsonObject;
    jsa : TJsonArray;
    jsp : TJsonPair;
begin
  try
    //create top-level object
    jsObj := TJsonObject.Create();
    //create an json-array
    jsa := TJsonArray.Create();
    //add array to object
    jsp := TJSONPair.Create('Array', jsa);
    jsObj.AddPair(jsp);

    //add items to the _first_ elemet of array
    jso := TJsonObject.Create();
    //add object pairs
    jso.AddPair(TJsonPair.Create('1', '1_1'));
    jso.AddPair(TJsonPair.Create('1_2_1', '1_2_2'));
    //put it into array
    jsa.AddElement(jso);

    //second element
    jso := TJsonObject.Create();
    //add object pairs
    jso.AddPair(TJsonPair.Create('x', 'x_x'));
    jso.AddPair(TJsonPair.Create('x_y_x', 'x_y_y'));
    //put it into array
    jsa.AddElement(jso);

    writeln(jsObj.ToString);
    readln;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

and output is

{"Array":[
     {"1":"1_1","1_2_1":"1_2_2"},
     {"x":"x_x","x_y_x":"x_y_y"}
  ]
}
teran
  • 3,214
  • 1
  • 21
  • 30
  • How do you free the objects created? I've a similar procedure, but when I call jsObj.Free an AV is raised, due to freeing the array elements. I'm looking for a working creation and freeing JSON Object embedding JSONarray embedding JSONObject example. – Giorgio Calzolato Jul 06 '23 at 13:43
-1

Same answer as @teran:

change:

JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1','1_1')));
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create('1_2_1','1_2_2')));

to:

JSONArray.AddElement(TJSONPair.Create('1','1_1'));
JSONArray.AddElement(TJSONPair.Create('1_2_1','1_2_2'));

Cheers.

umlcat
  • 4,091
  • 3
  • 19
  • 29
  • The `AddElement` method expects a `TJSONValue`, but `TJSONPair` is not a subclass of `TJSONValue`, so your suggested code won't even compile. The desired output is for the array to contain a value. You're trying to add pairs directly to the array, but pairs are not values. So, in what way is your answer the same as Teran's? – Rob Kennedy May 11 '12 at 20:58
  • @Rob Kennedy Ok, got an error, the idea was remove the extra level object – umlcat May 13 '12 at 01:38