Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-08

Copy TJSonArray to Array

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Chris Moody

Hi all,

First off thanks to all who have provided help over the past few weeks, I just have one last thing I'm trying to unravel (that I know of currently) and this project should be done.

The JSON feed I'm reading will have 2 arrays in it, would it be best to copy these to a normal array and then process the data? Or best to step through the TJSONArray?

I'm not sure how to copy a TJSONArray to an Array, I tried: A:=
E.Value.AsArray; but that of course did not work.

Any suggestions are as normal greatly appreciated,

Chris

Michael Van Canneyt
to Chris Moody
Chris Moody wrote 9 lines
Hi all, First off thanks to all who have provided help over the past few weeks, I just have one last thing I'm trying to unravel (that I know of currently) and this project should be done. The JSON feed I'm reading will have 2 arrays in it, would it be best to copy these to a normal array and then process the data? Or best to step through the TJSONArray?

if there is going to be a lot of processing, you might be better off copying the data. The JSON structure does incur some processing overhead.

I'm not sure how to copy a TJSONArray to an Array, I tried: A:= E.Value.AsArray; but that of course did not work.

A regular loop is simplest:

Var
   MyArray : TMyArray;
   MyJSONArray : TJSONArray;
   i : Integer;
begin
   MyJSONArray:=// Get the array...
   SetLength(MyArray,MyJSONArray.Count);
   For I:=0 to MyJSONArray.Count-1 do
     MyArray[i]:=MyJSONArray.Integers[i];
   // Now work with myarray
end.

Use the correct property for whatever type the array contains.

Michael.

Chris Moody
to Michael Van Canneyt
Michael Van Canneyt wrote 41 lines
On Wed, 12 Aug 2015, Chris Moody wrote: > Hi all, > > First off thanks to all who have provided help over the past few > weeks, I just have one last thing I'm trying to unravel (that I know > of currently) and this project should be done. > > The JSON feed I'm reading will have 2 arrays in it, would it be best > to copy these to a normal array and then process the data? Or best to > step through the TJSONArray? if there is going to be a lot of processing, you might be better off copying the data. The JSON structure does incur some processing overhead. > > I'm not sure how to copy a TJSONArray to an Array, I tried: A:= > E.Value.AsArray; but that of course did not work. A regular loop is simplest: Var MyArray : TMyArray; MyJSONArray : TJSONArray; i : Integer; begin MyJSONArray:=// Get the array... SetLength(MyArray,MyJSONArray.Count); For I:=0 to MyJSONArray.Count-1 do MyArray[i]:=MyJSONArray.Integers[i]; // Now work with myarray end. Use the correct property for whatever type the array contains. Michael. _______________________________________________

Hi Michael,

Thanks or the feedback. Just to make sure I'm understanding this, here is some more information, part of the JSON feed I'm working with will look like this:

"add":["URL1","URL2","URL3"]

My intention was to be able to get the keys under "add" and turn it into an array.

So if I'm understanding this correctly, I have to do something like the following:

var
J:TJSONData;
A1:TJSONArray;
A2:Array of String;
E : TJSONEnum;
begin
// Put JSONData into J
for E in J do
   if E.Key = 'add' then
       //Somehow copy add from J to A1
      // Use your code to copy A1 into A2
end;

Is this the right way of looking at it?

I have checked what is available online and not finding a lot as far as how to work with JSON, hopefully this will improve. Once I finish this software, I'd be more than happy to help in donating time for making new Wiki pages as far as this subject is concerned.

Thanks again,

Chris

Michael Van Canneyt
to Chris Moody
Chris Moody wrote 32 lines
> _______________________________________________ Hi Michael, Thanks or the feedback. Just to make sure I'm understanding this, here is some more information, part of the JSON feed I'm working with will look like this: "add":["URL1","URL2","URL3"] My intention was to be able to get the keys under "add" and turn it into an array. So if I'm understanding this correctly, I have to do something like the following: var J:TJSONData; A1:TJSONArray; A2:Array of String; E : TJSONEnum; begin // Put JSONData into J for E in J do if E.Key = 'add' then //Somehow copy add from J to A1 // Use your code to copy A1 into A2 end; Is this the right way of looking at it?

Yes, but you can do it more simple:

Var
   J : TJSONData;
   MyArray : Array of string;
   MyJSONArray : TJSONArray;
   i : Integer;
begin
   // Put JSONData into J
   MyJSONArray:=(J as TJSONObject).Arrays['add'];
   SetLength(MyArray,MyJSONArray.Count);
   For I:=0 to MyJSONArray.Count-1 do
     MyArray[i]:=MyJSONArray.Strings[i];
end.
Show quoted text 5 lines
I have checked what is available online and not finding a lot as far as how to work with JSON, hopefully this will improve. Once I finish this software, I'd be more than happy to help in donating time for making new Wiki pages as far as this subject is concerned.

There is a lot of code out there that works with JSON.

I wrote an article long time ago that describes the JSON support:

http://www.freepascal.org/~michael/articles/webdata/webdata.pdf

Page 11 and following.

the fpjson unit is documented:
http://www.freepascal.org/docs-html/fcl/fpjson/index.html

Hope this helps,

Michael.