2015-08-28 41 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是随机访问 - 没有任何顺序。

相关问题