如何从JSON响应中获取字段值

时间:2014-06-23 19:08:50

标签: ios json census

我可以从我的API中检索整个JSON响应并将其存储在NSArray中。

然后我遍历整个响应并将每个结果组存储在NSDictionary和NSLogit中。

但是,我不知道如何从每个组中获取字段值,如

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);
    }

我想做的就是得到像

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

这样我就可以从sep组中获取每个值。

这就是我的JSON数据在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
)
.
.
.
.
.
.
.
.
.

工作:

我得到了它,但它绝对不是最有效的方法。

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);
}

2 个答案:

答案 0 :(得分:0)

您从服务器返回的数据看起来像是一个数组,而不是一个字典数组。你想做这样的事情。

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);
}

答案 1 :(得分:0)

这是一种常见的方法,而不是为每个对象重复键,Web服务将回答多行,其中第一行包含键(列名称),后续行表示对象(值数组映射的位置)到列)。要产生您对输入的期望,请考虑以下事项:

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];
}