How do I make Java return a HTTP header in JSON as a Key Value Array
<p>I have the following as part of a method that is basically grabbing a webpage, I want to map the resulting header and body to JSON, the body is spat out as a string but ideally I want the header values split into key value so in the JavaScript I can access them directly.</p>

<pre><code>Map&lt;String, String&gt; assoc = new HashMap&lt;String, String&gt;();

Map&lt;String, List&lt;String&gt;&gt; headerMap = new LinkedHashMap&lt;String, List&lt;String&gt;&gt;();
headerMap = connection.getHeaderFields();

for (String key : headerMap.keySet()) {
    assoc.put(key, headerMap.get(key).toString());
}

JSONObject returnObj = new JSONObject();
returnObj.put("header", assoc);
returnObj.put("body", sb.toString());

return returnObj.toString(); //Set digit to add indent spacing.
</code></pre>

<p>This unfortunately returns the header as a string rather than an array...</p>

<pre><code>{"header":"{cache-control=[max-age=0], content-type=[text\/html], connection=[Keep-Alive],
</code></pre>

<p>Ideally this would be more like (friendlier for javascript)...</p>

<pre><code>{
    "headers": [
        {"test": "testval"},
        {"testb": "testbval"}
    ]
}
</code></pre>
