ios - add InvoicID new key and value in json data -


here json data http://pastie.org/10988178 want add invoice id in particular item.like if 1 invoice have 3 item item each item array add particular invoiceid

"itemdetails":[ { "itemid":1, "itemtype":1, "invoicedetailid":1, "upc":"upccode1", "sku":"item 1", "description":"item description", "qty":12, "punchlistpart":"item part new if return item", "comment":"comment1", "barcode":"" "invoiceid":95 }, 

i want output

here code not work

nslog(@"item=%lu",[[ret_val valueforkey:@"itemdetails" ] count]);     nsarray *testarray=[ret_val valueforkey:@"itemdetails" ];     nsmutablearray *item=[[nsmutablearray alloc]init];     for(int i=0; i<[[ret_val valueforkey:@"itemdetails" ] count]; i++) {          nsmutabledictionary *dictvalues = [nsmutabledictionary dictionarywithdictionary:testarray[i]];         [dictvalues setvalue:[[ret_val objectforkey:@"invoiceid"] objectatindex:i] forkey:@"invoiceid"];         [item addobject:dictvalues];     }     nslog(@"item=%@",item); 

ret_val nsmutabledictionary in json data.

the problem ret_val root json dictionary , want invoiceid inside data array of ret_val dictionary. need access dataarray can reduce code using for each loop.

nsmutablearray *items = [[nsmutablearray alloc] init]; nsarray *dataarray = [ret_val objectforkey:@"data"]; (nsdictionary *dic in dataarray) {      //now loop through `itemdetails` array.      nsarray *itemdetails = [dic objectforkey:@"itemdetails"];      (nsdictionary *itemdic in itemdetails) {           nsmutabledictionary *dictvalues = [nsmutabledictionary dictionarywithdictionary:itemdic];           [dictvalues setvalue:[dic objectforkey:@"invoiceid"] forkey:@"invoiceid"];           [items addobject:dictvalues];      } } nslog(@"%@", items) 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -