シリアライゼーション用の辞書を準備してから、サーバーに投稿する必要があります。ディクショナリには、@"items" キーの値として他のディクショナリがいくつかある場合があります。しかし、いくつかのブラケットが中断します。サーバーはエラーhtmlを返します。
NSMutableArray *a = [[NSMutableArray alloc]init];
for(int i = 0; i < [self.cartCopy count]; i++) {
NSString *itemNumber = [NSString stringWithFormat:@"%d", i + 1];
NSDictionary *tempDict = @{ itemNumber : @{
@"item_id" : [[self.cartCopy objectAtIndex:i]objectForKey:@"id"],
@"quantity" : [[self.cartCopy objectAtIndex:i]objectForKey:@"quantity"],
@"type" : [[self.cartCopy objectAtIndex:i]objectForKey:@"type"],
@"color_id" : @"0",
}
};
[a addObject:tempDict];
}
NSDictionary *dict = @{
@"date":oDate,
@"address":oAddress,
@"name":oName,
@"shipping_date":oShippingDate,
@"receiver_phone":oReceiverPhone,
@"customer_phone":oCustomerPhone,
@"total_price": oTotalPrice ,
@"additional_info": @"asd",
@"items": a
};
更新: [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] 後の文字列の NSLog :
{"address":"asd",
"name":"asd",
"receiver_phone":"123",
"customer_phone":"123",
"total_price":"1",
"date":"2013-03-05 21:22:55",
"additional_info":"asd",
"items":[
{"1":{
"type":"2",
"color_id":"0",
"item_id":10,
"quantity":"3"
}
},
{"2":{
"type":"1",
"color_id":"0",
"item_id":74,
"quantity":"3"
}
}
],
"shipping_date":"2030-03-03 12:12:12"
}
その理由は角括弧だと思います。どうすれば削除できますか?
たとえば、辞書で完全に機能します。
NSDictionary *dict = @{
@"date":oDate,
@"address":oAddress,
@"name":oName,
@"shipping_date":oShippingDate,
@"receiver_phone":oReceiverPhone,
@"customer_phone":oCustomerPhone,
@"total_price": oTotalPrice ,
@"additional_info": @"asd",
@"items": @{
@"1":@{
@"type":@"1",
@"color_id":@"0",
@"item_id":@"1",
@"quantity":@"1"
},
@"2":@{
@"type":@"1",
@"color_id":@"0",
@"item_id":@"1",
@"quantity":@"1"
}
}
};