-3

I have the following JSON!

This JSON wrote my bear drunk vodka :D

{
    "Label": [ 1, 2, 3, 4, 5 ],
    "ViewId": 1
}

code:

NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];  
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (int i=0; i < json.count; i++)
{
    NSString * FRid = [[json objectAtIndex:i] objectForKey:@"ViewId"]; //it's work
    NSString * FRName = [[json objectAtIndex:i] objectForKey:@"Label"]; //it's don't work   Out of scope

How I can get data from "Label" to NSString?

2 Answers2

0

Try:

NSString * FRid = [[json objectAtIndex:i] objectForKey:@"ViewId"]; 
NSArray * FRName = [[json objectAtIndex:i] objectForKey:@"Label"]; 

*Label Key contains an array, not a string.

And after this you can convert the array to string by following,

NSString *FRNameString = [FRName componentsJoinedByString:@", "];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSArray *label = [dict objectForKey:@"Label"];

//convert to string

NSString *final = [[NSString alloc]init];
for (NSString * string in label){
    final = [NSString stringWithFormat:@"%@%@", final, string];
}
NSLog(@"%@",final);

This is very close pseudocode

I wrote this on my phone, so i can't format as code.

JDS
  • 1,173
  • 12
  • 26
William Falcon
  • 9,813
  • 14
  • 67
  • 110