From Clomosy Docs
TCLJSONPair represents a single key–value association within a JSON object. It functions as the fundamental element of TCLJSONObject, encapsulating both the property name (key) and its corresponding TCLJSONValue (value).
This class provides a structured interface for examining, modifying, and transferring key/value pairs. It is primarily utilized when accessing individual object members, iterating through object data, or transferring property definitions between JSON structures.
TCLJSONPair ensures that each key and its associated value maintain valid JSON syntax and data type integrity. It forms the basis for key/value management within the TCLJSON framework, supporting consistent and predictable JSON object manipulation.
| Feature | Use of | Definition |
|---|---|---|
| AddJsonPair | obj.AddJsonPair(pair); | Allows manual insertion of a predefined key/value pair into a TCLJSONObject. Useful when dynamically constructing or reusing pairs. |
| GetValueAsPair | pair = obj.GetValueAsPair(0); | Returns a TCLJSONPair reference representing both the key and value of a JSON property. Can be used to inspect or modify elements sequentially. |
| Important! |
| Modifying a TCLJSONPair directly does not automatically update the parent TCLJSONObject unless you reassign the modified pair back. |
Example: TCLJSONPair in Use
var
Form1: TClForm;
ShowPairsBtn: TClProButton;
OutputMemo: TClMemo;
Obj: TCLJSONObject;
Pair: TCLJSONPair;
i: Integer;
void CreateJSONObject;
{
Obj = TCLJSONObject.Create;
Obj.AddPair('Username', TCLJSONValue.Str('ClomosyUser'));
Obj.AddPair('Age', TCLJSONValue.Int(26));
Obj.AddPair('Active', TCLJSONValue.Bool(True));
Obj.AddPair('Role', TCLJSONValue.Str('Editor'));
OutputMemo.Lines.Text = 'Object created with 4 pairs.';
}
void ShowPairs;
var
valStr: string;
{
OutputMemo.Lines.Clear;
for (i = 0 to Obj.Count - 1)
{
Pair = Obj.GetValueAsPair(i);
if (Pair.Value.IsString)
valStr = Pair.Value.AsString;
else if (Pair.Value.IsNumber)
valStr = FloatToStr(Pair.Value.AsFloat);
else if (Pair.Value.IsBoolean)
{
if (Pair.Value.AsBoolean)
valStr = 'True';
else
valStr = 'False';
}
else
valStr = '(Unknown Type)';
OutputMemo.Lines.Add(Pair.Key + ' = ' + valStr);
}
}
{
Form1 = TClForm.Create(Self);
OutputMemo = Form1.AddNewMemo(Form1, 'OutputMemo', 'Pairs Output');
OutputMemo.Align = alClient;
OutputMemo.Margins.Top = 10;
OutputMemo.Margins.Right = 10;
OutputMemo.Margins.Left = 10;
OutputMemo.Margins.Bottom = 10;
ShowPairsBtn = Form1.AddNewProButton(Form1, 'ShowPairsBtn', 'Show Pairs');
ShowPairsBtn.Align = alBottom;
ShowPairsBtn.Margins.Top = 10;
ShowPairsBtn.Margins.Left = 10;
ShowPairsBtn.Margins.Right = 10;
ShowPairsBtn.Margins.Bottom = 10;
ShowPairsBtn.OnClick = 'ShowPairs';
CreateJSONObject;
Form1.Run;
}
Output:
See Also