0

I'm parsing an XML and saving result to NSMutableArray. When I do NSLog,

NSLog(@"Data : %@",_data);

I'm getting

Data : (
        {
        SessionToken = 9e72dd029e0e8268380b919356881935;
    }
)

I only want 9e72dd029e0e8268380b919356881935 from the array. What is the best solution to achieve this?

EDIT : There will be only one SessionToken at a time.

6
  • Your data is an array of dictionary. You just need to use "SessionToken" key and you will get the value. Commented Apr 22, 2013 at 13:33
  • Will there be only one sessionToken at all times? Commented Apr 22, 2013 at 13:44
  • An NSDictionary can only map one object to one instance of a key. Commented Apr 22, 2013 at 13:50
  • @Anupdas no, sessionToken differs all the time. Commented Apr 22, 2013 at 13:53
  • @jack My question was if _data array contain more than one sessionToken at a time? Commented Apr 22, 2013 at 13:54

4 Answers 4

6

You can try this code :

for (NSDictionary *data1 in _data) {
    NSlog("session token %@",[data1 objectForKey:@"SessionToken"]);//Other wise add into another array which contain session token.. only..
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting this, expected ']'
1

Since there will be only one session at a time.

NSDictionary *session = [_data lastObject];
NSString *sessionToken = session[@"SessionToken"];

OR with literals

NSString *sessionToken = _data[0][@"SessionToken"];

1 Comment

@Viral As per the OP, there will be only one object in the array. If I don't use literals i always use lastObject as it will not crash if there are no objects. The second alternative was to show how this can be done with literals thats it.
1
if ([_data count]) {
    NSDictionary *dic = [_data objectAtIndex:0];
    NSLog(@"Data : %@",[dic objectForKey:@"SessionToken"]);
}

Comments

0
for (NSDictionary *data1 in _data) {
    NSlog("session token %@",[data1 valueForKey:@"SessionToken"]);
}

2 Comments

You could potentially expand this answer / explain it a bit more.
@Rogue it returns the value associated with a given key.if your json value is array with in array use like this [[data1 valueForKey:@"SessionToken"]objectAtIndex:0]

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.