-1

Editing for clarification

The userID is generated on the server side, so I don't actually retrieve the userID until after I POST the newly registered username and password. Like so:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSError *error;

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        userName.text, @"userName",
                        password.text, @"password",
                        nil];

NSData *data = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];

NSLog(@"PARAMS = %@", params);

[request setHTTPMethod:@"POST"];

[request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];

[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:data];

NSURLResponse *response = nil;

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *responseString = [[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:result options:0 error:&error];

NSString *results = json[@"result"];
if ([results isEqualToString:@"ok"])
{
    [self uploadS3Photo];
    NSLog(@"RESULT = %@", responseString);

}

}

End of Edit

I'm not quite sure how to grab the value userID, and post it back as an NSMutableString.

What I get back from the database is this

2014-02-23 20:52:48.029 TestData[245923:92c] RESULT = {"result":"ok","userId":26}

That number "26", I need to store and then call back to reverse its number, using a method like below.

-(NSString *)reverseString:(NSString *)str {
    NSMutableString *reversed = [NSMutableString string];
    NSInteger charIndex = [str length];
    while (charIndex > 0) {
        charIndex--;
        NSRange subRange = NSMakeRange(charIndex, 1);
        [reversed appendString:[str substringWithRange:subRange]];
    }
    return reversed;
}
f00d
  • 581
  • 1
  • 6
  • 21
  • 1
    Get it out of the dictionary you're printing. This has nothing to do with NSLog. – jscs Feb 25 '14 at 21:39
  • (I was wrong -- see below) "RESULT" is an NSDictionary from somewhere. The value of "userid" in the dictionary is an NSNumber. None of these is a NSString, so you have me confused. – Hot Licks Feb 25 '14 at 21:39
  • 1
    Well, actually no. "RESULT" is a JSON string. You need to decode the JSON through NSJSONSerialization. – Hot Licks Feb 25 '14 at 21:41
  • (If you don't know what JSON is you're probably in over your head, but see json.org.) – Hot Licks Feb 25 '14 at 21:46
  • I edited my question for a better understanding. Sorry about that! – f00d Feb 25 '14 at 22:11
  • 1
    So you got `result` back from the server, and have already decoded it into the `json` dictionary. You even fetched "result" out of the dictionary. And you can't figure out how to fetch "userId"???? (Keep in mind that the value of "userId" will be an NSNumber.) – Hot Licks Feb 25 '14 at 22:40

2 Answers2

3

You have a string with JSON content. To get the number from it you have to use NSJSONSerialization.

Here is how to do it:

NSData *data = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSNumber *myNumber = dict[@"RESULT"][@"userId"];

Then you can convert it to string:

NSString *stringWithNumber = [myNumber stringValue];
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
1

Sigh--

NSInteger userId = json[@"userId"].integerValue;
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • You're right. I'm sorry. Its been a stressful day today. Cannot even think correctly. I appreciate it – f00d Feb 25 '14 at 22:46