-1

I am trying to create nested jsonArray so that I can post this to server.

I want to post this type of json to server:

{
"topnode": {
    "node1": {
        "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
       
    },
    "node2": {
        "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
    },
    "node3": {
        "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
    },
    "node4": {
        "node4_1": [
            {
                "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "2", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "3", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ],
        "Node4_2": [
            {
                 "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "2", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ],
        "Node4_3": [
            {
                "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "2",  
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ]
    },
    "Node5": {
        "A": [
            {
                 "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "2", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "3", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ],
        "B": [
            {
                 "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                 "id": "2", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ],
        "C": [
            {
                 "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
                "id": "2", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ],
        "D": [
            {
                 "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            },
            {
               "id": "1", 
        "SecurityQuestion": "1",
        "SecuirtyAnswer": "11",
        "CreatedDate": "2015-03-16 11:30:59",
        "ModifiedDate": "2015-03-16 11:30:59",
        "AuthDate": "2015-03-16 11:30:59"
            }
        ]
    }
}
}

I am not getting any idea how to do this. Here is what I am trying:

        JSONArray obj_tArray = new JSONArray();
                additionalArrayData  = new JSONArray();
             for (salary tSalary : tsalary_arrayOfList) { 
                 
                    additionalData.put("employer", tSalary .getemployer());
                    additionalData.put("name", tSalary .getname());
                    additionalData.put("income_head_salary", tSalary .getincome_head_salary());
                    additionalData.put("total", tSalary .gettotal());
                    additionalData.put("year", tSalary .getYear());
                    additionalData.put("panCardNumber", tSalary .getUserPanCard());
                    additionalData.put("income_head_salary", tSalary .getincome_head_salary());
                                        
                    obj_tArray.put(additionalData); 
                } 
         
            additionalArrayData.put("t_salary"); 
            additionalArrayData.put(1, obj_TArray);    
halfer
  • 18,701
  • 13
  • 79
  • 158
Banku
  • 287
  • 3
  • 24

1 Answers1

1

Something like this should work:

JSONObject innerObject = new JSONObject();
JSONObject middleObject = new JSONObject();
JSONObject outerObject = new JSONObject();

innerObject.put("id", "1");
innerObject.put("SecurityQuestion", "TestQuestion");

middleObject.put("node1", innerObject);
outerObject.put("topnode", middleObject);

This is how you get nested JSONObjects. You need to add your logic for iterating over the different objects now.

Deutro
  • 2,605
  • 2
  • 12
  • 24