【问题标题】:How to store all data in single array so that I can easily post it as request?如何将所有数据存储在单个数组中,以便我可以轻松地将其作为请求发布?
【发布时间】:2024-01-07 20:35:01
【问题描述】:

我正在尝试创建嵌套的 jsonArray,以便将其发布到服务器。

我想将这种类型的 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);    

【问题讨论】:

  • 到目前为止你在哪里?
  • 我正在尝试这样@eduyayo
  • 我可以创建请求,但不知道如何将所有这些节点放在一个数组中。 :(@eduyayo

标签: android json post multidimensional-array httprequest


【解决方案1】:

这样的事情应该可以工作:

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 的方式。您现在需要添加用于迭代不同对象的逻辑。

【讨论】:

  • 感谢您为我提供路径@Deutro