English 中文(简体)
Delphi: JSON
原标题:Delphi: JSON array
  • 时间:2012-05-11 10:49:24
  •  标签:
  • json
  • delphi

努力了解德尔菲的JSON。 使用“DBXJSON.pas”模块。 • 如何利用它来使这种阵列:

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);

但要:

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

Please help! Thanks!

最佳回答

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.

产出

{"Array":[
     {"1":"1_1","1_2_1":"1_2_2"},
     {"x":"x_x","x_y_x":"x_y_y"}
  ]
}
问题回答

回答如下:

变化:

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

:

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

Cheers.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签