2015-08-28 43 views
1

我有數組裏面存儲的JSON values.when我試着給這個值字典給定的錯誤。如何從數組中提供json值到字典?

arrhole{ 
     "cust_groups" = ""; 
     "customer_id" = 1; 
     "customer_name " = ""; 
     "date_added" = "<null>"; 
     "delivery_method" = ""; 
     fname = kishore; 
     gift = ""; 
     lname = kumar; 
     message = ""; 
     month = ""; 
     "order_id" = 1; 
     "pay_method" = ""; 
     "payment_firstname" = ""; 
     "payment_lastname" = ""; 
     phone = 9043563659; 
     region = ""; 
     reward = ""; 
     "scheduled_date" = "28/08/2015"; 
     "sen_email" = ""; 
     "sen_name" = ""; 
     "shipping_city" = ""; 
     "shipping_company" = ""; 
     "shipping_country" = IND; 
     "shipping_region" = 1503; 
     "unit_price" = 22; 
     voucher = ""; 
    } 

此代碼我已經使用:

-(void)responseFunction:(NSMutableDictionary *)response 
    { 
     NSLog(@"response method%@",response); 
     BOOL success; 
     success =[[response objectForKey:@"success"] boolValue]; 
     arrHoleOrderDetails =[response objectForKey:@"order"]; 
     NSLog(@"arrhole%@",arrHoleOrderDetails); 
     if(success) 
     { 
      for (NSDictionary *dic in arrHoleOrderDetails) 
      { 

       NSLog(@"dic===%@",dic); 
      } 
     } 

OUTPUT:

dic===city 
dic===shipping_address 
dic===shipping_lastname 
dic===pay_method 
dic===customer_id 
dic===country 
dic===region 

這樣它的顯示在我的log.i顯示這裏只有一些價值。

1.是否爲json響應問題?

2.或者我的愚蠢的錯誤?

完全響應:

order =  { 
     address = ""; 
     address1 = ""; 
     address2 = ""; 
     affliate = ""; 
     amount = ""; 
     city = ""; 
     comment = ""; 
     company = ""; 
     "company_id" = ""; 
     country = ""; 
     coupon = ""; 
     "cust_groups" = ""; 
     "customer_id" = 2; 
     "customer_name " = ""; 
     "date_added" = "<null>"; 
     "delivery_method" = ""; 
     email = ""; 
     fax = ""; 
     fname = arun; 
     gift = ""; 
     lname = ""; 
     message = ""; 
     month = ""; 
     "order_id" = 2; 
     "pay_method" = ""; 
     "payment_firstname" = ""; 
     "payment_lastname" = ""; 
     phone = ""; 
     postcode = ""; 
     product = 4; 
     quantity = 5; 
     "rec_email" = ""; 
     "rec_name" = ""; 
     region = ""; 
     reward = ""; 
     "scheduled_date" = "28/08/2015"; 
     "sen_email" = ""; 
     "sen_name" = ""; 
     "shipping_address" = ""; 
     "shipping_address1" = ""; 
     "shipping_address2" = ""; 
     "shipping_city" = ""; 
     "shipping_company" = ""; 
     "shipping_country" = ""; 
     "shipping_firstname" = ""; 
     "shipping_lastname" = ""; 
     "shipping_postcode" = ""; 
     "shipping_region" = ""; 
     status = ""; 
     stores = ""; 
     total = ""; 
     "unit_price" = 540; 
     voucher = ""; 
    }; 
    success = 1; 
} 
+0

你到底想要什麼?因爲你有一串字符串 – iPhone

+0

你想如何處理這個arrHoleOrderDetails數組? – iPhone

+0

這是我存儲在數組中的json響應,我需要在標籤中顯示,所以我將值從數組傳遞到字典,但是我提到的輸出如上所示。 –

回答

1

嘗試下面的代碼

-(void)responseFunction:(NSMutableDictionary *)response 
     { 
      NSLog(@"response method%@",response); 
      BOOL success; 
      success =[[response objectForKey:@"success"] boolValue]; 
      arrHoleOrderDetails =[response objectForKey:@"order"]; 
      NSLog(@"arrhole%@",arrHoleOrderDetails); 
      if(success) 
      { 
       yourlable.text = [arrHoleOrderDdetails valueForKey:@"address"]; 
       yourlabel2.text = [arrHoleOrderDdetails valueForKey:@"address1"]; 

//In case of integer value try following 
yourlbl3.text = [NSString stringWithFormat:@"%d",[arrHoleOrderDdetails valueForKey:@"address1"]]; 

    //go ahead in same manner. 

    } 
+0

請解釋我的json格式有什麼不對,因爲我需要通知它bro @iphone –

+0

如果您有任何整數值,則將其轉換爲字符串。 – iPhone

+0

其實你有字符串不是字典數組,所以不需要使用for循環。您可以直接將這些值分配給您的UILabel – iPhone

0

這應該工作:

if(success) 
{ 
    //display all keys and values 
    for (NSString* key in [arrHoleOrderDetails allKeys]) 
    { 
     NSString* value = [arrHoleOrderDetails objetForKey:key]; 
     NSLog(@"%@===%@",key, value); 
    } 

    //access single value 
    NSLog(@"%@", arrHoleOrderDetails[@"scheduled_date"]); 
} 

如果迭代上的NSDictionary,需要迭代上的按鍵和獲取值給定的關鍵。 還要記住,NSDictionaries是隨機訪問 - 沒有任何順序。

相關問題