title: Build JSON with Delphi
body: <p>I'm trying to implement a function which returns a json object containing elements of class. Here is my function:</p>
  
  <pre><code>procedure ListToJson(AInputList: TList&lt;TRating&gt;;
    AResponse: TJSONObject);
  var
    i: Integer;
    jsonPair: TJSONPair;
    jsonObject: TJSONObject;
    jsonArray: TJSONArray;
  begin
    jsonArray := TJSONArray.Create();
    jsonObject := TJSONObject.Create();
    jsonPair := TJSONPair.Create('ratings', jsonArray);
  
    for i := 0 to AInputList.Count - 1 do
    begin
      jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
      jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
      jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
      jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
      jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
  
      jsonArray.AddElement(jsonObject);
    end;
  
    AResponse.AddPair(jsonPair);
   end;
  </code></pre>
  
  <p>And when I test it with a list which contains two elements the returned string is:</p>
  
  <pre><code>{
    "ratings":[{
        "idrating":"1",
        "idmark":"0",
        "value":"0",
        "description":"",
        "timeposted":"2015-07-29 11:25:03",
        "idrating":"2",
        "idmark":"0",
        "value":"0",
        "description":"",
        "timeposted":"2015-07-29 11:25:24"
    },{
        "idrating":"1",
        "idmark":"0",
        "value":"0",
        "description":"",
        "timeposted":"2015-07-29 11:25:03",
        "idrating":"2",
        "idmark":"0",
        "value":"0",
        "description":"",
        "timeposted":"2015-07-29 11:25:24"
    }]
  } 
  </code></pre>
  
  <p>I tried to remove all pairs after each loop iteration:</p>
  
  <pre><code>procedure ListToJson(AInputList: TList&lt;TRating&gt;;
    AResponse: TJSONObject);
  var
    i: Integer;
    jsonPair: TJSONPair;
    jsonObject: TJSONObject;
    jsonArray: TJSONArray;
  begin
    jsonArray := TJSONArray.Create();
    jsonObject := TJSONObject.Create();
    jsonPair := TJSONPair.Create('ratings', jsonArray);
  
    for i := 0 to AInputList.Count - 1 do
    begin
      jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
      jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
      jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
      jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
      jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));
  
      jsonArray.AddElement(jsonObject);
      jsonObject.RemovePair('idrating');
      jsonObject.RemovePair('idmark');
      jsonObject.RemovePair('value');
      jsonObject.RemovePair('description');
      jsonObject.RemovePair('timeposted');
    end;
  
    AResponse.AddPair(jsonPair);
   end;
  </code></pre>
  
  <p>And the ouput is an array with n(count of list elements) empty objects: <code>{"ratings":[{},{}]}</code></p>
  
  <p>And the json I'm trying to build should be like:</p>
  
  <pre><code>{
    "ratings":[{
      "idrating":"1",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:03"
    },{
      "idrating":"2",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:24"
    }]
  }
  </code></pre>