Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6721361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:19:16+00:00 2026-05-26T09:19:16+00:00

I have the following as part of a method that is basically grabbing a

  • 0

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.

Map<String, String> assoc = new HashMap<String, String>();

Map<String, List<String>> headerMap = new LinkedHashMap<String, List<String>>();
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.

This unfortunately returns the header as a string rather than an array…

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

Ideally this would be more like (friendlier for javascript)…

{
    "headers": [
        {"test": "testval"},
        {"testb": "testbval"}
    ]
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:19:17+00:00Added an answer on May 26, 2026 at 9:19 am
    JSONObject returnObj = new JSONObject();
    for (String key : headerMap.keySet()) {
        returnObj.put(key, headerMap.get(key).toString());
    }
    return returnObj.toString();
    

    I have not used this api before, but from the javadoc I am pretty sure the code above will give you what you want. Since you are only dealing with one set of data (the header object), this method will work. If you had many objects, you would have to do something different. Try flexjson, that is one I use often.

    Also, the reason you are getting your current output is the JSONObject.put is calling the Map’s toString() which produces a string of all key/value pairs represented as “key=value” separated by a comma and wrapped in curly brackets.

    {"key1=value1", "key2=value2", etc..}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following part of a function that is driving me crazy. As
I have the following ugly if statement that is part of a class that
I have the following string: h3. My Title Goes Here I basically want to
I have a method that looks similar to the following: public void myMethod(MyClass c)
I have the following method that is triggered when an exception occurs in a
I have the following code that defines a getParts method to find a given
I have the following part in my Page_Load method: DropDownList1.Items.Add(Audi); DropDownList1.Items.Add(BMW); … DropDownList1.Items.Add(Ford); …
I have created a web service that is basically a json set of values
I have the following class (part of it): class SearchViewModel : BaseViewModel<SearchResultItem> { private
Thanks in advance for your assistance. I have the following exported part: [Export (typeof(INewComponent))]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.