objective c - how to create a json object in ios -
i want post value in json format server stuck json format. have searched alot on google still didnt exact format.
{ "accountid": "14", "order_items": [ { "item_id": "5", "quantity": "4 kg", "price": " 69.99" }, { "item_id": "6", "quantity": "6 kg", "price": " 79.99" } ] }
i tried following code:
nsdictionary *dict=@{@"item_id" : itemide, @"quantity" : @"10", @"price" : @"100"}; nsdictionary *dictionary = @{@"accountid" : @"14", @"order_items" : dict}; nserror *error; nsdata *postdata = [nsjsonserialization datawithjsonobject:dictionary options:0 error:&error]; if (error) nslog(@"failure serialize json object %@", error); nsurl *url = [nsurl urlwithstring:@"http://api.lococo.in/orders"]; nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:180.0]; [request sethttpmethod:@"post"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request setvalue:@"agdhfgh243sfs" forhttpheaderfield:@"x-auth-user"]; [request setvalue:@"abcgdg#dg" forhttpheaderfield:@"x-auth-token"]; [request sethttpbody:postdata];
please me it. how in above format?
you missing order_items array.
nsdictionary* item1 = @{ @"item_id": @"5", @"quantity": @"4 kg", @"price": @" 69.99" }; nsdictionary* item2 = @{ @"item_id": @"6", @"quantity": @"6 kg", @"price": @"79.99" }; nsarray* orderitems = @[item1, item2]; nsdictionary* order = @{ @"accountid": @"24", @"order_items": orderitems };
and that's pass nsjsonserialization.
Comments
Post a Comment