我必须准备一个序列化字典,然后将其发布到服务器。 Dictionary可能有其他几个字典作为@“items”键的值。但有些括号会中断。和服务器响应我一个错误的HTML。将字典添加到nsdictionary作为密钥的值
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
};
UPDATE:我的字符串的NSLog后[NSJSONSerialization dataWithJSONObject:字典选项:kNilOptions错误:零]:
{"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"
}
}
};
你遇到了什么问题? – Ares 2013-03-05 14:57:25
如何删除干扰括号。我更新了问题 – Asike 2013-03-05 15:00:16
方括号表示一个数组。您需要与服务器开发人员进行沟通,以了解他们需要的JSON格式。格式将是数据的名称(键)和数据类型(值)的列表。 – Fogmeister 2013-03-05 15:46:13