2013-03-05 47 views
0

我必須準備一個序列化字典,然後將其發佈到服務器。 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" 
            } 
           } 
        }; 
+0

你遇到了什麼問題? – Ares 2013-03-05 14:57:25

+0

如何刪除干擾括號。我更新了問題 – Asike 2013-03-05 15:00:16

+0

方括號表示一個數組。您需要與服務器開發人員進行溝通,以瞭解他們需要的JSON格式。格式將是數據的名稱(鍵)和數據類型(值)的列表。 – Fogmeister 2013-03-05 15:46:13

回答

0

在您的例子完美的作品的項目對象是與鍵的字典{1,2} 。

在您輸出的JSON中,您的items對象是一個數組。

該數組包含2個對象,每個對象都是一個字典。

第一個包含一個密鑰{1}。 第二個包含一個鍵{2}。

您只需要移除數組並使用字典來存儲這些字典。

0

這看起來好像要發送JSON到服務器。你可以從你的字典創建JSON數據與

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

如果你需要它作爲一個字符串:

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
+0

是的,我這樣做之後。但之前我需要準備我的字典「字典」,因爲我的格式是錯誤的。 – Asike 2013-03-05 15:11:46

+0

@Asike:字典是鍵/值對的集合。你不能「準備」字典格式。您看到的括號只是調試器或NSLog()*顯示字典的方式。 - 請嘗試使用此代碼將字典轉換爲JSON。如果它不起作用,請更新您的問題。 – 2013-03-05 15:14:46

+0

好的,我會嘗試 – Asike 2013-03-05 15:21:47