2011-04-29 41 views
1

我有一個NSDictionary,我不知道如何檢索「@mode」和「@route」(這是從JSON響應)的值。NSDictionnary檢索值

我該怎麼做?

謝謝!

這是的NSDictionary:

{ 
"@mode" = WALK; 
"@route" = "Rue University"; 
distance = "17.513516908661757"; 
duration = 13000; 
endTime = "2011-04-29T12:08:59.395-04:00"; 
from =  { 
    geometry = "{\"type\": \"Point\", \"coordinates\": [-73.57120386807037,45.503820214186405]}"; 
    lat = "45.503820214186405"; 
    lon = "-73.57120386807037"; 
    name = "Rue University"; 
}; 
legGeometry =  { 
    length = 3; 
    points = "{mvtG`k``[email protected]"; 
}; 
startTime = "2011-04-29T12:08:46.395-04:00"; 
steps =  { 
    walkSteps =   (
        { 
      absoluteDirection = NORTHWEST; 
      becomes = false; 
      distance = "17.513516908661757"; 
      elevation = ""; 
      lat = "45.503820214186405"; 
      lon = "-73.57120386807037"; 
      stayOn = false; 
      streetName = "Rue University"; 
     }, 
        { 
      absoluteDirection = NORTH; 
      becomes = true; 
      distance = "0.0"; 
      elevation = ""; 
      lat = "45.50390573910769"; 
      lon = "-73.57139257694809"; 
      relativeDirection = RIGHT; 
      stayOn = false; 
      streetName = "street transit link"; 
     } 
    ); 
}; 
to =  { 
    geometry = "{\"type\": \"Point\", \"coordinates\": [-73.571363,45.503971]}"; 
    lat = "45.503971"; 
    lon = "-73.571363"; 
    name = "Station McGill"; 
    stopId = 32; 
}; 
+0

這看起來更像是一個字典而不是數組。 – Caleb 2011-04-29 16:19:21

回答

2

看起來物體在索引0處是一個的NSDictionary。

NSDictionary *dict = [array objectAtIndex:0]; 
NSString *route = [dict objectForKey:@"@route"]; 
+0

現在很簡單!謝謝! – Maxime 2011-04-29 16:31:03

+0

@Maxime不客氣! – albertamg 2011-04-29 16:32:04

1

試試這個:

[myDictionary objectForKey:@"@mode"]; 
+0

在「@mode」之前添加一個「@」使其@「@ mode」。關鍵需要是一個對象,所以你應該在這裏使用一個常量NSString而不是一個C字符串。 – Caleb 2011-04-29 16:34:04

0

給定一個NSDictionary dict,你可以檢索使用對應的鍵@mode值:

[dict objectForKey:@"@mode"]; 
0

兩種可能性: 1)NSMutableArray的是JSON格式字符串數組。 2)NSMutableArray是NSDictionaries的數組。

如果其#1,那麼您需要使用可可JSON的東西的JSON字符串轉換成一個NSDictionary解碼:

NSDictionary* aDict = [[myArray objectAtIndex:0] JSONValue]; 

OR

NSDictionary* aDict = [theArray objectAtIndex:0]; 

的JSON的東西可以在這裏http://code.google.com/p/json-framework/

然後這應該工作:

NSString* theMode = [aDict objectForKey:@"@mode"]; 
NSLog(@"the mode is %@, as read from the dictionary %@", theMode, aDict); 

--Tom