i want frame json , response server. i'm having 1 problem, need send response of arrays of objects containing 3 objects. me..
this json array i'm having:
{ "checklistgrid": [ { "chklst_dtl_seq_no": 141, "answer": "pass" }, { "chklst_dtl_seq_no": 142, "answer": "pass" }, { "chklst_dtl_seq_no": 143, "answer": "pass" }, { "chklst_dtl_seq_no": 144, "answer": "pass" }, { "chklst_dtl_seq_no": 145, "answer": "pass" }, { "chklst_dtl_seq_no": 146, "answer": "pass" }, { "chklst_dtl_seq_no": 147, "answer": "pass" } ] }
below format want send :
{ "checklistgrid_array": [ { "seq_no": "1", "answer": "pass", "chklst_dtl_seq_no": "141", "seq_no_nxt": "2", "answer_nxt": "", "chklst_dtl_seq_no_nxt": "142", "seq_no_lst": "3", "answer_lst": "", "chklst_dtl_seq_no_lst": "143" }, { "seq_no": "4", "answer": "pass", "chklst_dtl_seq_no": "144", "seq_no_nxt": "5", "answer_nxt": "", "chklst_dtl_seq_no_nxt": "145", "seq_no_lst": "6", "answer_lst": "", "chklst_dtl_seq_no_lst": "146" }, { "seq_no": "7", "answer": "pass", "chklst_dtl_seq_no": "147", "seq_no_nxt": "", "answer_nxt": "", "chklst_dtl_seq_no_nxt": "", "seq_no_lst": "", "answer_lst": "", "chklst_dtl_seq_no_lst": "" } ] }
i.e. want send 3 objects within 1 object, , 3 objects in object need send server in above
{ "checklistgrid_array": [{141,142,143},{144,145,146},{147,148,149},{150,"",""}] }
if understand correctly, want loop through original array, , combine every 3 objects , rename dictionary keys.
no language specified, it's write complete code. swift given example below
- access
checklistgrid
array, e.g.jsondata["checklistgrid"]
- prepare new array
checklistgrid_array
- have counter
seq_no
, starting 1 - loop through array , increment counter, e.g.
for detail in checklistgrid
- depending on counter value, generate new dictionary or add existing one, e.g.
if counter % 3 == 1 {dict["seq_no"]=counter ...}
- check counter value after loop, make sure fill in blank ones, e.g.
if counter % 3 == 2 {dict["seq_no_nxt"]=counter; dict["answer_nxt"]=""; dict["chklst_dtl_seq_no_nxt"]=""}
- assign created
checklistgrid_array
new dictionary converted , sent in json.
Comments
Post a Comment