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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:48:42+00:00 2026-06-04T01:48:42+00:00

import net.sf.json.*; public class JSONDemo { /** * @param args */ public static void

  • 0
import net.sf.json.*;

public class JSONDemo {

/**
 * @param args
 */
public static void main(String[] args) {
    JSONObject mainObj = new JSONObject();

    JSONObject jObj1 = new JSONObject();
    JSONObject jObj2 = new JSONObject();

    JSONArray jA1 = new JSONArray();        
    JSONArray jA2 = new JSONArray();

    JSONArray mainArray= new JSONArray();

    jObj1.accumulate("id", 17);
    jObj1.accumulate("name", "Alex");
    jObj1.accumulate("children", jA1);

    mainArray.add(jObj1);

    jObj2.accumulate("id", 94);
    jObj2.accumulate("name", "Steve");
    jObj2.accumulate("children", jA2);

    //Adding the new object to jObj1 via jA1

    jA1.add(jObj2);

    mainObj.accumulate("ccgs", mainArray);
    System.out.println(mainObj.toString());     
}    

}

The output I got is

{"ccgs":[{"id":17,"name":"Alex","children":[]}]}

I wanted the jObj2 within the children key of jObj1.

  • 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-06-04T01:48:43+00:00Added an answer on June 4, 2026 at 1:48 am

    Apparently the node creation order has an impact on the generated string. If you change the object creation order, beginning with the children, the Json is correct.

    See that code :

        public static void main(String[] args) {
            // first create the child node
            JSONObject jObj2 = new JSONObject();
            jObj2.accumulate("id", 94);
            jObj2.accumulate("name", "Steve");
            jObj2.accumulate("children", new JSONArray());
    
            // then create the parent's children array
            JSONArray jA1 = new JSONArray(); 
            jA1.add(jObj2);
    
            // then create the parent
            JSONObject jObj1 = new JSONObject();
            jObj1.accumulate("id", 17);
            jObj1.accumulate("name", "Alex");
            jObj1.accumulate("children", jA1);
    
            // then create the main array
            JSONArray mainArray = new JSONArray();
            mainArray.add(jObj1);
    
            // then create the main object
            JSONObject mainObj = new JSONObject();
            mainObj.accumulate("ccgs", mainArray);
    
            System.out.println(mainObj);    
        }
    

    The output is :

    {"ccgs":[{"id":17,"name":"Alex","children":[{"id":94,"name":"Steve","children":[]}]}]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code: import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; ... public void testDateSerializer() { final Date date
Here's my code package net.sourceforge.jwebunit.sample; import net.sourceforge.jwebunit.WebTestCase; public class JWebUnitSearchExample extends WebTestCase { public
Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
<%@ page language=java import=net.sf.json.JSONArray %> <% JSONArray arrayObj=new JSONArray(); arrayObj.add(MCA); arrayObj.add(Amit Kumar); arrayObj.add(19-12-1986); arrayObj.add(24);
The following is my Class code import java.net.*; import java.util.*; import java.sql.*; import org.apache.log4j.*;
how to translate this to C# import java.io.*; import java.net.*; class SimpleServer { private
I'm using tomcat 5 and JSON parser net/sf/json . I import json-lib2.4-jdk15.jar in my
I'm completely new to both VB.NET and JSON, and I'm trying to figure out
i have written a class which implements ListFieldCallBack like, import java.util.Vector; import net.rim.device.api.ui.Graphics; import
In Scala 2.8 and liftweb.net 2.0 I'm trying to serialize a case-class to Json

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.