title: What's the fastest way to build a JSON string in java?
body: <p>I'm working with JSON on the server for the first time. I'm trying to create an object that looks like this:</p>
  
  <pre><code>{
    columnData : {
      column1 : {"foo": "bar", "value" : 1 },
    },
    rowData : {
      row1 : {"type" : "integer",  "value" : 1},
    }
  }
  </code></pre>
  
  <p>Essentially, it's a two-level JSON object in which I've got all objects on the top level. Everything on the second level will be a string, and integer, or a float. I've found several ways I could go about creating this JSON string:</p>
  
  <ul>
  <li><strong>Build the string directly</strong></li>
  </ul>
  
  <p>I can go through one line at a time and build up a giant string. This seems unwieldy.</p>
  
  <ul>
  <li><strong>Use gson</strong></li>
  </ul>
  
  <p>I can build a map (or class?) that has the structure of the JSON I want, and then use gson to convert to a JSON string.</p>
  
  <ul>
  <li><strong>Use JsonObject</strong></li>
  </ul>
  
  <p>This would work like <a href="https://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject">this question.</a> I just found that example, and I like how simple working with JSON looks in it, but I'm not sure if it's a better way to go than the other options.</p>
  
  <p>I'm pretty new with Java, so I'm not really sure what the performance tradeoffs of these methods are. I do know that I'll potentially be calling this method lots of times very quickly to load data into a client as the user scrolls a table in a webapp, so I'd like everything to be as fast as possible. What's the quickest way to go about building the JSON string that I'll then be sending back to the client?</p>