0

I'm new to objective c. There are a couple of object which i want to take out from this array results with is from JSON and i need help. How do i get objects out from this array? how can i get the features then the attributes and route name etc.

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

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

The object that i want to get from

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

how can i do it?

2
  • What is the type of jsonResult? Commented Jul 2, 2012 at 12:25
  • the jsonresults that i got is the direction={...} mention abv Commented Jul 2, 2012 at 12:29

2 Answers 2

2

[directions objectAtIndex: i] returns NSDictionary, if you want to get objects from it, do the following

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

And so on

Sign up to request clarification or add additional context in comments.

6 Comments

hmm.. do i have to put the object as NSDictionary?
the directions array already contains a dictionary, you will just need to get the values out of it
thx. for the features is it an array? cos i have to get the attributes
featues is an nsdictionary too, you can get each value inside it using valueForKey too
[dic valueForKey:@"attributes"]; and it got me a null how should i get it?
|
1

each object inside directions is an NSDictionary, and each object inside them is also a dictionary. So you will need something like this:

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

...and so on, until you get all the values.

3 Comments

can't work it got this error -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xde3c210
Please post code on how you get jsonResult. It seems you're trying to access an NSArray as a dictionary.
responseData = [[NSMutableData data] retain]; NSURL *url = [NSURL URLWithString:format]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:result options:kNilOptions error:&error];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.