how to frame a JSON Array in ios -


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

  1. access checklistgrid array, e.g. jsondata["checklistgrid"]
  2. prepare new array checklistgrid_array
  3. have counter seq_no, starting 1
  4. loop through array , increment counter, e.g. for detail in checklistgrid
  5. depending on counter value, generate new dictionary or add existing one, e.g. if counter % 3 == 1 {dict["seq_no"]=counter ...}
  6. 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"]=""}
  7. assign created checklistgrid_array new dictionary converted , sent in json.

Comments