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

  • SEARCH
  • Home
  • 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 5951339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:31:20+00:00 2026-05-22T17:31:20+00:00

I think (actually I KNOW!) I’m doing something wrong here I am trying to

  • 0

I think (actually I KNOW!) I’m doing something wrong here I am trying to populate some values into HashMap and add each hasmap to a list which will be added to a JSON object:

JSONObject json = new JSONObject();
try
{
    Map address;
    List addresses = new ArrayList();

    int count = 15;

    for (int i=0 ; i<count ; i++)
    {
        address = new HashMap();
        address.put("CustomerName"     , "Decepticons" + i);
        address.put("AccountId"        , "1999" + i);
        address.put("SiteId"           , "1888" + i);
        address.put("Number"            , "7" + i);
        address.put("Building"          , "StarScream Skyscraper" + i);
        address.put("Street"            , "Devestator Avenue" + i);
        address.put("City"              , "Megatron City" + i);
        address.put("ZipCode"          , "ZZ00 XX1" + i);
        address.put("Country"           , "CyberTron" + i);
        addresses.add(address);
    }
    json.put("Addresses", addresses);
}
catch (JSONException jse)
{

}
response.setContentType("application/json");
response.getWriter().write(json.toString());

My problem is I know this is returning a string, which I cannot seem to parse (which is the problem). My question is how do I return the actual JSON encoded string (or even should I be doing this?) or what is the best method of attack for this type of problem. The JavaScript I am using for this is below:

function getReadyStateHandler(req)
{
    // Return an anonymous function that listens to the
    // XMLHttpRequest instance
    return function ()
    {
        // If the request's status is "complete"
        if (req.readyState == 4)
        {
            // Check that a successful server response was received
            if (req.status == 200)
            {
                msgBox("JSON Response recieved...");
                populateDatagrid(req.responseText.toJSON());
            }
            else
            {
                // An HTTP problem has occurred
                alert("HTTP error: " + req.status);
            }
        }
    }
}

Note the JSON Response comes back fine, but its a string. Any advice is greatly appreciated. I am also opening to using googles Gson, but don’t have too much knowledge on that.

  • 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-22T17:31:21+00:00Added an answer on May 22, 2026 at 5:31 pm

    Got it working! I should have been building a JSONArray of JSONObjects and then add the array to a final “Addresses” JSONObject. Observe the following:

    JSONObject json      = new JSONObject();
    JSONArray  addresses = new JSONArray();
    JSONObject address;
    try
    {
       int count = 15;
    
       for (int i=0 ; i<count ; i++)
       {
           address = new JSONObject();
           address.put("CustomerName"     , "Decepticons" + i);
           address.put("AccountId"        , "1999" + i);
           address.put("SiteId"           , "1888" + i);
           address.put("Number"            , "7" + i);
           address.put("Building"          , "StarScream Skyscraper" + i);
           address.put("Street"            , "Devestator Avenue" + i);
           address.put("City"              , "Megatron City" + i);
           address.put("ZipCode"          , "ZZ00 XX1" + i);
           address.put("Country"           , "CyberTron" + i);
           addresses.add(address);
       }
       json.put("Addresses", addresses);
    }
    catch (JSONException jse)
    { 
    
    }
    response.setContentType("application/json");
    response.getWriter().write(json.toString());
    

    This worked and returned valid and parse-able JSON. Hopefully this helps someone else in the future. Thanks for your help Marcel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason my code here (this is the entire thing) doesnt actually render
I'm trying to design an async framework and wanted to know what people think
I know everyone who reads the question will think Firebug! right away. Maybe some
I don't think I can actually add a field (column) to an existing IEnumerable.
Does anyone actually think this is a good reason to Dumb down your code?
Let's say, hypothetically (read: I don't think I actually need this, but I am
I actually have two questions regarding the same problem but I think it is
I think most people know how to do this via the GUI (right click
I think most people here understand the importance of fully automated builds. The problem
I think this is a relatively simple question, but I don't precisely know what's

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.