简体   繁体   中英

Adding multiple JSONObjects to JSONArray

I have a JSONObject which gets data from a dao call whenever a form is filled by the user. So, I needed to store these multiple JSONObjects to a JSONArray without any of the data from previous JSONobject entry being lost. How do I go about doing this?

Also, needed a way to parse each induvial JSONObjects from this array.

Please note that the JSONObject contains may fields of data. I'll attach a sample below.

{"contents":[{"sectionTitle":"Basic Info","index":0,"sectionId":"AU-BASIC-INFO","fields":[{"name":"username","type":"input","value":"akhil"},{"name":"fullName","type":"input","value":"tester12"},{"name":"email","type":"input","value":"test@ecs.com"},{"name":"phone","type":"input","value":""},{"name":"role","options":[{"label":"testing","value":"931924ba-d1c5-4025-bf1c-01394ed3233a"},{"label":"check","value":"eef0b1d2-e468-4c70-a261-2e0b1001d816"},{"label":"Creditor_Debtor_Maker_Authorizer__Self_Admin","value":"5b5265f7-d883-4810-9dc1-ce313da971da"},{"label":"SuperAdmins","value":"88ob1be1-e80d-4189-962p-f60d667d02c8"},{"label":"Test Ecs admin","value":"c945184e-c044-4b15-9316-44a07b5fe0b3"},{"label":"Administrator","value":"b7ff1740-7dfc-434c-8d50-bb06f34ace19"},{"label":"Authorizer","value":"bf643fbb-c673-4023-b9d8-5d2163228723"}],"type":"input","value":"931924ba-d1c5-4025-bf1c-01394ed3233a","isDisplay":true},{"name":"userProfile","options":[{"label":"Administrator","value":"56068bb3-845f-415e-9c10-57e6e0ebf038"},{"label":"Tenaga User profile","value":"bedd8da6-615e-4e0c-8e0b-4a9a99e84888"},{"label":"Tenaga Admin profile","value":"a62d69b5-004a-4c58-83a8-39d009166ba8"},{"label":"Tnadmin Profile","value":"8ef85fd5-7a0c-41ae-a0d7-a77905f35b42"}],"type":"input","value":"56068bb3-845f-415e-9c10-57e6e0ebf038","isDisplay":true}]},{"sectionTitle":"Password","index":1,"sectionId":"AU-PASSWORD","fields":[{"name":"passwordType","options":[{"label":"Manual Generate","value":"01"},{"label":"Auto Generate","value":"02"}],"type":"input","value":"01","isDisplay":true},{"name":"changePassword","type":"input","value":""},{"name":"confirmPassword","type":"input","value":""}]},{"sectionTitle":"Preference","index":2,"sectionId":"AU-PREFERENCE","fields":[{"name":"locale","type":"input","value":"en-us","isDisplay":true},{"name":"dateFormat","type":"input","value":"dd-MM-yyyy","isDisplay":true},{"name":"timeFormat","type":"input","value":"","isDisplay":true},{"name":"numberFormat","type":"input","value":"","isDisplay":true}]}],"clientCode":"BNPP","recId":"f423c108-f920-4c66-a3e2-0dd8939e66b7","corporateId":"04011000008"}

To parse JSONArray , use this method toJavaList :

for (JSONObject json : jsonArray.toJavaList(JSONObject.class)) {
            
}

How to recognize that data in json has lost?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM