如何将所有数据存储在单个数组中,以便我可以轻松地将其作为请求发布?

Posted

技术标签:

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

我正在尝试创建嵌套的 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 ***.com/questions/6218143/… 我可以创建请求,但不知道如何将所有这些节点放在一个数组中。 :(@eduyayo 【参考方案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

以上是关于如何将所有数据存储在单个数组中,以便我可以轻松地将其作为请求发布?的主要内容,如果未能解决你的问题,请参考以下文章

使用 laravel 4 上的循环将数据数组存储到单个数组中

如何有效地将单个对象的多个 C++ 数据成员公开给 QML?

如何简单地将关卡数据存储为数组并再次加载?

如何轻松地将存储添加到安装了 XP 的 VirtualBox 机器?

如何编辑 GitHub 发布日期?

如何更有效地将词汇存储在数组中?