从NSDictionary检索值

时间:2011-04-29 16:14:02

标签: iphone objective-c

我有一个NSDictionary,我不知道如何检索“@mode”和“@route”的值(这是来自json响应)。

我怎么能这样做?

谢谢!

这是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``MOd@ME";
};
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;
};

4 个答案:

答案 0 :(得分:2)

看起来索引0处的对象是NSDictionary。

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

答案 1 :(得分:1)

试试这个:

[myDictionary objectForKey:@"@mode"];

答案 2 :(得分:0)

给定NSDictionary dict,您可以使用以下方法检索与密钥@mode对应的值:

[dict objectForKey:@"@mode"];

答案 3 :(得分:0)

两种可能性: 1)NSMutableArray是一个JSON格式的字符串数组。 2)NSMutableArray是NSDictionaries的数组。

如果它是#1,那么你需要使用Cocoa 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);

- 汤姆