Jump to content
Sign in to follow this  
alnickels

JSON array name.

Recommended Posts

I have a JSON object that has two arrays.  How can I retrieve the name of the array before I loop through all of the roes / elements?  I am not sure if this is called an array name or a key. 

Share this post


Link to post

An array is just an element, like any other. A Json Object is made up of pairs. Every pair has a name and a value. A value can be an array, text, float, ...

 

Here is an example:

program Project1;

uses System.SysUtils, System.JSON;

const
	input =
		'{'+
		'	"someNumbers": [1,2,3],'+
		'	"justOneNumber": 42,'+
		'	"some Texts": ["Hello World", "My body is ready"]'+
		'}';
var
	jsonObject: TJsonObject;
	pair: TJsonPair;
begin
	jsonObject := TJsonObject.ParseJSONValue(input) as TJsonObject;
	for pair in jsonObject do
		if(pair.JsonValue is TJsonArray) then
			WriteLn('We found an array called "', pair.JsonString.Value(), '"');

	ReadLn;
end.

 

It will output the following:

We found an array called "someNumbers"
We found an array called "some Texts"

 

Edited by Der schöne Günther

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×