如何将所有数据存储在单个数组中,以便我可以轻松地 post 它作为请求?
How to store all data in single array so that I can easily post it as request?
我正在尝试创建嵌套的 json 数组,以便我可以 post 将其发送到服务器。
我想post这种json服务器:
{
"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"
}
]
}
}
}
我不知道该怎么做。这是我正在尝试的:
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);
像这样的东西应该可以工作:
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);
这就是您获取嵌套 JSONObject 的方式。您现在需要添加迭代不同对象的逻辑。
我正在尝试创建嵌套的 json 数组,以便我可以 post 将其发送到服务器。
我想post这种json服务器:
{
"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"
}
]
}
}
}
我不知道该怎么做。这是我正在尝试的:
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);
像这样的东西应该可以工作:
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);
这就是您获取嵌套 JSONObject 的方式。您现在需要添加迭代不同对象的逻辑。