title: Delphi parse JSON array or array
body: <p>This is the sample JSON I want to be able to parse:</p>
  
  <pre class="lang-json prettyprint-override"><code>[
    {
      "a":{
        "username":"aaa",
        "email":"aaa@gmail.com"
      }
    },
    {
      "b":{
        "username":"bbb",
        "email":"bbb@gmail.com"
      }
    }
  ]
  </code></pre>
  
  <p>I need that a call to <code>getData('b', 'email')</code> must output <code>bbb@gmail.com</code>!</p>
  
  <hr>
  
  <p>I am really struggling to understand how to use the <code>System.JSON</code> unit, but I can't get the solution! I want to be able to write a function that extracts a particular data from the above kind of JSON structure. This is my code so far. In the class constructor I have:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>var
    FIds: TJSONArray;
  begin
    FIds := TJSONObject.ParseJSONValue({json string here}) as TJSONArray;
  end;
  </code></pre>
  
  <p>Then, inside the function that must return the data, I have written this:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>// 'name' can be 'a' or 'b'  | 'data' can be 'username' or 'email'
  function TTest.getData(const name, data: string): string;
  var
    FValue, FValueInner: TJSONValue;
  begin
    for FValue in Fids do
    begin
      if (FValue is TJSONArray) then
      begin
        //Here I am sure that I have a TJSONArray (which can be 'a' or 'b' from above)
      end;
    end;
  end;
  </code></pre>
  
  <p>According to what I have written above, I have to check the value of <code>name</code> and decide if I have to access the data inside <code>a</code> or <code>b</code>. Then, once I have picked the correct JSON array <code>a</code> or <code>b</code>, I have to select if I want to display the <code>username</code> or the <code>email</code> field (which is specified inside the <code>data</code> variable). </p>
  
  <p>How can I do this?</p>
  
  <hr>
  
  <p>This is my latest attempt but I really can't understand what to do:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>... same code above ...
  
  if (FValue is TJSONArray) then
  begin
    //here I want to check if the current array is a or b
    if ((FValue as TJSONArray).Items[0] as TJSONValue).Value = name then
    begin
      Farr := TJSONObject.ParseJSONValue(((FValue as TJSONArray).Items[0] as TJSONValue).ToJSON) as TJSONArray;
      try
        //here I want to get the data inside username or email
        for FValueInner in Farr do
          Result := FValueInner.GetValue&lt;string&gt;(data);
      finally
        Farr.Free;
      end;
    end;
  end;
  </code></pre>
  
  <p>Where <code>Farr: TJSONArray;</code> and <code>FValueInner: TJSONValue;</code></p>