title: Creating JSON inside a loop in .Net
body: <p>I'm trying to get this code to work dynamically but I can't.<br />
  I need to add more lines, placing this code inside a <code>for</code> loop.</p>
  <p>My actual working code is:</p>
  <pre><code>Dim objAtrib as object = New With {Key .attributes = {
      New With {Key .id = &quot;MODEL&quot;, Key .value_name = &quot;RT1&quot;},
      New With {Key .id = &quot;BRAND&quot;, Key .value_name = &quot;SAM&quot;}
  }}
  </code></pre>
  <p>I need some like this:</p>
  <pre><code>For i as integer = 0 to r.count
    Dim objAtrib As Object = New With {Key .attributes = {
        New With {Key .id = i, Key .value_name = i}
    }}
  Next
  </code></pre>
  <p>Any help will be appreciated.<br />
  The JSON result should be like this:</p>
  <pre><code>{
     &quot;attributes&quot;:[
        {
           &quot;id&quot;:&quot;Z&quot;,
           &quot;value_name&quot;:&quot;0&quot;
        },
        {
           &quot;id&quot;:&quot;A&quot;,
           &quot;value_name&quot;:&quot;1&quot;
        },
        {
           &quot;id&quot;:&quot;B&quot;,
           &quot;value_name&quot;:&quot;2&quot;
        },
        {
           &quot;id&quot;:&quot;C&quot;,
           &quot;value_name&quot;:&quot;3&quot;
        },
        {
           &quot;id&quot;:&quot;D&quot;,
           &quot;value_name&quot;:&quot;4&quot;
        },
        {
           &quot;id&quot;:&quot;F&quot;,
           &quot;value_name&quot;:&quot;5&quot;
        },
        {
           &quot;id&quot;:&quot;G&quot;,
           &quot;value_name&quot;:&quot;6&quot;
        }
     ]
  }
  </code></pre>
  <p>The class Model is:</p>
  <pre><code> Public Class Attributes
          Public Property id As String
          Public Property value_name As String
      End Class
  
      Public Class RootObjectAtributosPublicar
          Public Property attributes As Attributes()
      End Class
  </code></pre>
  <p>This doesn't work:</p>
  <pre><code>For m As Integer = 0 To grid.RowCount
      Dim id As String = &quot;&quot;
      Dim valor As String = &quot;&quot;
  
      id = grid.GetRowCellValue(m, &quot;id&quot;)
      valor = grid.GetRowCellValue(m, &quot;Valor&quot;)
  
      objAtrib.attributes(m).id = id
      objAtrib.attributes(m).value_name = valor
   Next
  </code></pre>