0

我必须准备一个字典进行序列化,然后将其发布到服务器。字典可能有几个其他字典作为 @"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"
                                    }
                               }
                    };
4

2 回答 2

0

看起来好像您想将JSON发送到服务器。您可以使用从字典创建 JSON 数据

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];

如果您需要它作为字符串:

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
于 2013-03-05T15:07:55.227 回答
0

在您完美运行的示例中,项目对象是带有键 {1, 2} 的字典。

在您的输出 JSON 中,您的 items 对象是一个数组。

该数组包含 2 个对象,每个对象都是一个字典。

The first contains a single key {1}. The second contains a single key {2}.

You just need to remove the array and use a dictionary instead to store these dictionaries.

于 2013-03-05T17:18:59.740 回答