Witam,

chciałbym uzyskać taki efekt:

[LISTA_DEFEKTOW]
  - "DEFEKT_1": "Cofnięty licznik"
    [ODCZYT_1]
    - "data_odczytu":"2016-01-01"
    - "przebieg":""10000"
    [ODCZYT_2]
    - "data_odczytu":"2016-01-01"
    - "przebieg":""10000"
    [ODCZYT_3]
    - "data_odczytu":"2016-01-01"
    - "przebieg":""10000"
  - "DEFEKT_2":"Kierownica po prawej stronie"
  - "DEFEKT_3":"kolejny item"

Niestety kombinuję, ale jedyne co udaje mi się otrzymać to:

'{"lista_defektow":[{"defekt_1":"Cofnięty licznik","odczyt_1":[{"data_odczytu":"data","przebieg":"100"}]},{"defekt_1":"Cofnięty licznik","odczyt_1":[{"data_odczytu":"data","przebieg":"100"}]},{"defekt_2":"Kierownica po prawej stronie"}]}'

EDIT:
Chyba mi się udało po modyfikacjach:

'{"lista_defektow":[{"defekt_1":"Cofnięty licznik","historia_licznika":[{"data_odczytu":"data","przebieg":"100"},{"data_odczytu":"data","przebieg":"100"}]},{"defekt_2":"Kierownica po prawej stronie"},{"defekt_3":"Kradziony"}]}'

Mam taki kod:

type
  TNewVIN = record
    JSON_DefectsList: TJSONObject;
  end;

function AddCounterHistory(AItem: TListBoxItem; AJSONObject: TJSONObject; AJSONArray: TJSONArray): TJSONObject;
var
  jsonPair: TJSONPair;
  jsonArray: TJSONArray;
  jsonObject: TJSONObject;
begin
  jsonObject := TJSONObject.Create();
  jsonObject.AddPair(TJSONPair.Create('data_odczytu', 'data'));
  jsonObject.AddPair(TJSONPair.Create('przebieg', '100'));
  AJSONArray.AddElement(jsonObject);

  Result := AJSONObject;
end;

procedure generateJSON
var
  i, j, CheckedCount, posNo: integer;
  lbItem: TListBoxItem;
  jsonArray_CounterHistory, jsonArray_Defects: TJSONArray;
  jsonPair_CounterHistory, jsonPair_Defects: TJSONPair;
  jsonObject: TList<TJSONObject>;
  s: string;
begin

  if Assigned(Rec_NewVIN.JSON_DefectsList) then
    FreeAndNil(Rec_NewVIN.JSON_DefectsList);

  Rec_NewVIN.JSON_DefectsList := TJSONObject.Create;

  btnOK.ModalResult := mrOk;

  jsonObject := TList<TJSONObject>.Create;
  jsonArray_CounterHistory := TJSONArray.Create();
  jsonArray_Defects := TJSONArray.Create();
  jsonPair_CounterHistory := TJSONPair.Create('historia_licznika', jsonArray_CounterHistory);
  jsonPair_Defects := TJSONPair.Create('lista_defektow', jsonArray_Defects);

  posNo := 1;
  try
    for i := 0 to ListBox1.Items.Count -1 do
    begin
      lbItem := ListBox1.ItemByIndex(i);
      if lbItem.IsChecked then
      begin
        jsonObject.Add(TJSONObject.Create());

        if lbItem.ItemData.Text = 'Cofnięty licznik' then
        begin
          jsonObject.Last.AddPair(TJSONPair.Create('defekt_' + posNo.ToString, lbItem.ItemData.Text));

          if lbCounterHistory.Count > 1 then
          begin
            // Iteracja po liście historii odczytów licznika
            jsonObject.Last.AddPair(jsonPair_CounterHistory);
            for j := 1 to lbCounterHistory.Count -1 do
              AddCounterHistory(lbItem, jsonObject.Last, jsonArray_CounterHistory); // Dodaj odczyt do tablicy

            jsonArray_Defects.AddElement(jsonObject.Last); // Dodaj tablice odczytow do tablicy defektow
          end
          else
            jsonArray_Defects.AddElement(jsonObject.Last);
        end
        else
        begin
          jsonObject.Last.AddPair(TJSONPair.Create('defekt_' + posNo.ToString, lbItem.ItemData.Text));
          jsonArray_Defects.AddElement(jsonObject.Last);
        end;


        Inc(posNo);
      end;
    end;
  finally
    jsonObject.Free;
  end;

  Rec_NewVIN.JSON_DefectsList.AddPair(jsonPair_Defects); 

  s := Rec_NewVIN.JSON_DefectsList.ToString; // dla podglądu z debuggera
end;