繁体   English   中英

从nsarray中获取对象

[英]getting object out from nsarray

我是目标c的新手。 我想从JSON中从这个数组结果中取出几个对象,我需要帮助。 我如何从该数组中取出对象? 我如何获取功能,然后获取属性和路线名称等。

NSArray *directions=[jsonResult objectForKey:@"directions"];

    int i;
    NSArray *dict;
    int count = [directions count];
    for (i = 0; i < count; i++)
    {            
        NSLog (@"directions = %@", [directions objectAtIndex: i]);          
    }

我想从中获取的对象

directions = {
features =     (
            {
        attributes =             {
            ETA = 1341190800000;
            length = 0;
            maneuverType = esriDMTDepart;
            text = "Start at 18304.680000,36152.730000";
            time = 0;
        };
        compressedGeometry = "+1+hrt+139j+0+0";
    },
            {
        attributes =             {
            ETA = 1341190800000;
            length = "1.43124650292492";
            maneuverType = esriDMTStraight;
            text = "Go southeast on PAN ISLAND EXPRESSWAY";
            time = "1.22675858855561";
        };
        compressedGeometry = "+1+hrt+139j+i9-a6+kp-bl";
    }
routeId = 1;
routeName = "18304.680000,36152.730000 - 29663.160389,40202.513760";
summary =     {
    envelope =         {
        spatialReference =             {
            wkid = 3414;
        };
        xmax = "29663.160018156";
        xmin = "18301.4360762186";
        ymax = "40229.9300290999";
        ymin = "35091.9900291003";
    };
    totalDriveTime = "24.8214824061658";
    totalLength = "17.2089251018779";
    totalTime = "24.8";
};

我该怎么做?

[directions objectAtIndex: i]返回NSDictionary ,如果要从中获取对象,请执行以下操作

NSDictionary *dic = [directions objectAtIndex: i];
[dic valueForKey:@"routeName"] //route name
[dic valueForKey:@"routeId"] //routeId
[dic valueForKey:@"features"] //returns an nsdictionery too
[[dic valueForKey:@"features"] valueForKey:@"text"] //returns an nsdictionery too

等等

指示内的每个对象都是NSDictionary ,指示内的每个对象也是字典。 因此,您将需要以下内容:

NSDictionary *directions=[jsonResult objectForKey:@"directions"];
NSDictionary *features = [directions objectForKey:@"features"];

...等等,直到获得所有值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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