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 6850341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:45+00:00 2026-05-27T01:06:45+00:00

I am facing issue in looping in the following code,i am passing a list

  • 0

I am facing issue in looping in the following code,i am passing a list of forms to it,i am not sure what is going wrong,i need the output as but i am getting only the last form_id.I have this code to get this output as ,here i am passing a list of forms and getting the json as output. Please let me know where am i going wrong.

output :

{
    "forms": [
        { "form_id": "1", "form_name": "test1" },
        { "form_id": "2", "form_name": "test2" } 
    ]
}

code :

public class MyFormToJSONConverter {

   public JSONObject getJsonFromMyFormObject(List<Form> form) {

        JSONObject responseDetailsJson = new JSONObject();
        JSONArray jsonArray = null;
        List<JSONArray> list = new ArrayList<JSONArray>();
        System.out.println(form.size());
        for (int i = 0; i < form.size(); i++) {
               JSONObject formDetailsJson = new JSONObject();
               formDetailsJson.put("form_id", form.get(i).getId());
               formDetailsJson.put("form_name", form.get(i).getName());
               formDetailsJson.put("desc", 
                                    form.get(i).getFormDescription());
               jsonArray = new JSONArray();
               jsonArray.add(formDetailsJson);
               list.add(jsonArray);
        }

        for (JSONArray json : list) {
               responseDetailsJson.put("form", json);
        }

        return responseDetailsJson;
   }
  • 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-27T01:06:46+00:00Added an answer on May 27, 2026 at 1:06 am

    Your problem is here:

    for (JSONArray json : list) {
         responseDetailsJson.put("form", json);
    }
    

    will overwrite all of the previous values with the next value (a single JSON object). You want

    responseDetailsJson.put("form", list);
    

    You probably should also get rid of this:

    jsonArray = new JSONArray();
    jsonArray.add(formDetailsJson);
    list.add(jsonArray);
    

    That will give you:

    {
        "forms": [
            [{ "form_id": "1", "form_name": "test1" }],
            [{ "form_id": "2", "form_name": "test2" }] 
        ]
    }
    

    All told, I think you want:

    JSONObject responseDetailsJson = new JSONObject();
    List<JSONObject> list = new ArrayList<JSONObject>();
    System.out.println(form.size());
    // List.get will be very inefficient if passed a LinkedList 
    // instead of an ArrayList.
    for (Form formInstance:form) {
           JSONObject formDetailsJson = new JSONObject();
           formDetailsJson.put("form_id", formInstance.getId());
           formDetailsJson.put("form_name", formInstance.getName());
           formDetailsJson.put("desc", 
                                formInstance.getFormDescription());
           list.add(formDetailsJson);
    }
    
    responseDetailsJson.put("form", list);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC2 with NHibernate, but am facing an issue. All calls
I am facing the following issue when deploying a com-exposed assembly to my client's.
I am facing issue with the following Json reponse object in the javascript eval
I am facing an issue with this simple jQuery code. I have attached the
I am facing strange issue with our WCF service. The same code was working
so I'm facing an issue here that I'm sure many a developer before me
I am facing an issue with FB Like button. I used below code to
I am facing an issue here as shown in code is mentioned below Inside
I'm facing a issue with opening mail client using openURL. Here is the code.
I am facing strange issue on Windows CE: Running 3 EXEs 1)First exe doing

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.