我们如何在NSstring ios中存储一堆json键值

时间:2016-02-20 13:13:29

标签: ios objective-c nsstring

  source =   {
            Address1 = "3244 W WASHINGTON ST , ";
            Address2 = "";
            City = "ANDERSON     ";
            Name = "<null>";
            PhoneNo = "<null>";
            ProviderId = "<null>";
            ProviderName = "<null>";
            State = IN;
            Zip = "46011-";
        };

嗨我是ios的新手,我正在将我的应用程序与服务集成,并且在收到服务的响应后,我将存储在字典值之上,例如我的下面的代码,但它显示异常

请帮助我如何将kay值存储在一个NSstring

我的代码: -

 NSString *SourceAdrees = [NSString stringWithFormat:@"%@\n %@ %@ %@" ,[[[[[dicCurrentIndex valueForKeyPath:@"source"]valueForKey:@"Address1"]valueForKey:@"City"]valueForKey:@"State"]valueForKey:@"Zip"]];

   NSLog(@"SourceAdrees is %@",SourceAdrees);

2 个答案:

答案 0 :(得分:1)

如果我理解你想做什么,代码看起来像这样:

NSDictionary *sourceDict = dicCurrentIndex[@"source"];
NSString *sourceAddress = [NSString stringWithFormat:@"%@\n %@ %@ %@", 
  sourceDict[@"Address1"], 
  sourceDict[@"City"], 
  sourceDict[@"State"], 
  sourceDict[@"Zip"] ];

(变量名称应以小写字母开头,单词为“address”,而不是“SourceAdrees”)

答案 1 :(得分:0)

如果要在一个字符串中显示值,则有一个名为

的方法
  

stringByAppending

使用即

NSString *SourceAdrees=[SourceAdrees stringByAppendingString:[dicCurrentIndex valueForKey: @"Address1"]stringByAppendingString:[dicCurrentIndex valueForKey:@"City"]]];