简体   繁体   中英

How to get field values from JSON response

I'm able to retrieve the entire JSON response from my API and store that in a NSArray.

Then I loop through the entire response and store each result group in NSDictionary and NSLogit.

However, I'm not how can I get the field values from within each group like

STNAME,
CTYNAME,
DENSITY,
POP,
DATE,
state,
county


- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSArray* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                         options:kNilOptions 
                                                           error:&error];

 for (int i=0; i<[json count]; i++) {
     NSDictionary *avatars = [json objectAtIndex:i];
      NSLog(@"value at %d, :::  %@", i,avatars);
    }

What I was trying to do is get something like

//        NSString *name = avatar[@"STNAME"];  // for any specific state or county name..like objectbyKey

so that I can get each value from the sep groups.

This is how my JSON data looks like on NSLog

2014-06-23 12:04:06.893 KivaJSONDemo[2693:90b] value at 0, :::  (
    STNAME,
    CTYNAME,
    DENSITY,
    POP,
    DATE,
    state,
    county
)
2014-06-23 12:04:06.894 KivaJSONDemo[2693:90b] value at 1, :::  (
    Florida,
    "Alachua County",
    "282.65234809",
    247336,
    1,
    12,
    001
)
2014-06-23 12:04:06.894 KivaJSONDemo[2693:90b] value at 2, :::  (
    Florida,
    "Alachua County",
    "282.65234809",
    247336,
    2,
    12,
    001
)
2014-06-23 12:04:06.895 KivaJSONDemo[2693:90b] value at 3, :::  (
    Florida,
    "Alachua County",
    "283.0454668",
    247680,
    3,
    12,
    001
)
2014-06-23 12:04:06.895 KivaJSONDemo[2693:90b] value at 4, :::  (
    Florida,
    "Alachua County",
    "285.30018541",
    249653,
    4,
    12,
    001
)
.
.
.
.
.
.
.
.
.

Working:

I got it working, but it's definitely not the most efficient way to do it.

NSString *value = @"CTYNAME";
NSUInteger idx = [json[0] indexOfObject:value];

NSString *value2=@"POP";
NSUInteger idx1 = [json[0] indexOfObject:value2];

NSString *value3=@"DENSITY";
NSUInteger idx2 = [json[0] indexOfObject:value2];


if (idx != NSNotFound)
{
    for (NSUInteger i = 1; i < [json count]; i=i+6)
    {
        if (idx < [json[i] count])
        {
            NSLog(@"County:%@             Population:%@            Density:%@", json[i][idx],json[i][idx1],json[i][idx2]);


            [CountyNames addObject:json[i][idx]];
            [CountyPopulation addObject:json[i][idx1]];
            [CountyDensity addObject:json[i][idx2]];

        }
        else
        {
            NSLog(@"Value %@ unavailable in %@", value, json[i]);
        }
    }
}
else
{
    NSLog(@"Value %@ not found.", value);
}

It looks like the data you're getting back from the server is coming as an array, not an array of dictionaries. You're looking to do something like this.

NSArray *avatars = @[@[@"STNAME",
                       @"CTYNAME",
                       @"DENSITY",
                       @"POP",
                       @"DATE",
                       @"state",
                       @"county"],
                     @[@"Florida",
                       @"Alachua County",
                       @"282.65234809",
                       @247336,
                       @1,
                       @12,
                       @001],
                     @[@"Florida",
                       @"Alachua County",
                       @"282.65234809",
                       @247336,
                       @2,
                       @12,
                       @001],
                     @[@"Florida",
                       @"Alachua County",
                       @"283.0454668",
                       @247680,
                       @3,
                       @12,
                       @001],
                     @[@"Florida",
                       @"Alachua County",
                       @"285.30018541",
                       @249653,
                       @4,
                       @12,
                       @001]];

NSString *value = @"DENSITY";
NSUInteger idx = [avatars[0] indexOfObject:value];
if (idx != NSNotFound)
{
    for (NSUInteger i = 1; i < [avatars count]; ++i)
    {
        if (idx < [avatars[i] count])
        {
            NSLog(@"%@", avatars[i][idx]);
        }
        else
        {
            NSLog(@"Value %@ unavailable in %@", value, avatars[i]);
        }
    }
}
else
{
    NSLog(@"Value %@ not found.", value);
}

It's a common approach, rather than repeating the keys for each object, the web service will answer several rows, where the first row contains keys (column names) and subsequent rows represent objects (where the array of values maps to the columns). To produce what you expect from the input, consider the following:

NSMutableArray *resultDictionaries = [NSMutableArray array];  // this will be our result
NSArray *keys = json[0];

for (int i=1; i<json.count; i++) {
    NSArray *row = json[i];
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:row forKeys:keys];
    [resultDictionaries addObject:dictionary];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM