2016-08-03 55 views
0

,所以我必须得到我的JSON数据中,我有:贮藏JSON数据作为一个字符串目标C

{ 
"name": "brad" 
} 

我想那个人名义存储为一个字符串。

@try 
{ 
    NSError *error; 
NSString *url_string = [NSString stringWithFormat: @"http://Share/scripts/newjson.json"]; 
///Dummy URL 
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]]; 
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"json: %@", json); 

} 
@catch (NSException *e) 
{ 
    NSLog(@"Internet not enabled"); 
    //something went wrong 
    //populate the NSError object so it can be accessed 
} 

但是,当我打印出来我只是得到如上所示什么JSON文件,我是否需要使用分隔符或东西来得到这个字符串?

由于

回答

2

其实JSON是字典不是一个数组并用nilOptions不可变。

要获得布拉德

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSString *name = json[@"name"]; 
NSLog(@"name: %@", name); 
+0

欢呼它的工作 – Brad