0

I am new to Objective-C and iOS development and am trying to write a method for generating a JSON string but when i NSLog this i get a bunch of hexcodes (i am guessing they are pointer adresses) can someone look at this method and tell me what i am doing wrong..

   -(NSData *)generateInputJSON
{

    NSError* error = nil;

    NSString* region = [[NSLocale currentLocale]localeIdentifier];

    NSString* language = [[NSLocale preferredLanguages]objectAtIndex:0];

    NSMutableDictionary* properties = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              _sphereODNumber,@"sphereOD",
                              _sphereOSNumber,@"sphereOS",
                              _cylinderODNumber,@"cylinderOD",
                              _cylinderOSNumber,@"cylinderOS",
                              _visionCorrection,@"visionCorrection",
                                       region,@"region",
                                       language,@"language",
                              nil];

    if([_visionCorrection isEqualToString:@"multiFocal"] ||
       [_visionCorrection isEqualToString: @"progressive"])
    {
        [properties setValue:_addODNumber forKey:@"addOD"];
        [properties setValue:_addOSNumber forKey:@"addOS"];
    }


    if([_visionCorrection isEqualToString: @"progressive"])
    {
        [properties setValue:_fittingHeightNumber forKeyPath:@"fittingHeight"];
    }

    NSData* inputJSON = [NSJSONSerialization dataWithJSONObject:properties options:NSJSONWritingPrettyPrinted error:&error];

    return inputJSON;
}

my Log returns:

<7b0a2020 226c616e 67756167 6522203a 2022656e 222c0a20 20227669 73696f6e 436f7272 65637469 6f6e2220 3a202270 726f6772 65737369 7665222c 0a202022 6164644f 4422203a 20342e35 2c0a2020 22616464 4f532220 3a20342e 352c0a20 20227370 68657265 4f532220 3a20342e 352c0a20 20227265 67696f6e 22203a20 22656e5f 5553222c 0a202022 63796c69 6e646572 4f532220 3a20342e 352c0a20 20226669 7474696e 67486569 67687422 203a2031 372c0a20 20227370 68657265 4f442220 3a20342e 352c0a20 20226379 6c696e64 65724f44 22203a20 342e350a 7d>

2 Answers 2

1

What you're seeing is a description of the NSData object, which is just the the bytes in the data in hex. If you were to (say) write that data to a file and open the file with a text editor you'd actually see the string you want. Since you want to return a string from your function, you can convert the NSData to a string like so:

return [[NSString alloc] initWithData:inputJSON encoding:NSUTF8StringEncoding];

Remember to autorelease that if you're in non-ARC code. You should also change the return type of your method from NSData* to NSString* if you're going to return a string.

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

Comments

1

Convert the data into an NSString like so

NSString *string = [[NSString alloc]initWithData:inputJSON encoding:NSUTF8StringEncoding];

then just print it with NSLog

NSLog(@"%@", string);

2 Comments

question really for both of you... When i pass this JSON to a Server do i need to convert to a string with the proper encoding or is it fine as NSData
No it should be fine to pass the NSData, you just need to set the correct encoding on the http calls so that the server knows it's receiving UTF8 data

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.